Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using .sel with a list of booleans, but for a boolean dtype dimension raises weird errors #9917

Closed
doronbehar opened this issue Jan 1, 2025 · 2 comments
Labels
needs triage Issue that has not been reviewed by xarray team member

Comments

@doronbehar
Copy link

doronbehar commented Jan 1, 2025

What is your issue?

Take this MWE:

#!/usr/bin/env python

import xarray as xr
import numpy as np

# Define coordinates
float_coords = np.linspace(0.0, 1.0, 5)  # Float coordinates from 0.0 to 1.0
bool_coords = np.array([True, False])    # Boolean coordinates
# Create the Dataset
example_dataset = xr.Dataset(
    coords={
        "float_dim": float_coords,
        "bool_dim": bool_coords,
    }
)
loc = {
    "float_dim": 0,
    "bool_dim": True,
}
print(example_dataset.sel({
    k: [v]
    for k,v in loc.items()
}))

It fails with:

Traceback (most recent call last):
  File "/home/doron/repos/lab-ion-trap-simulations/./t.py", line 22, in <module>
    print(example_dataset.sel({"bool_dim": [ True ]}))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/r2g9s5hcmndxk6lwxwavpg3ga33sf18c-python3-3.12.7-env/lib/python3.12/site-packages/xarray/core/dataset.py", line 3237, in sel
    result = self.isel(indexers=query_results.dim_indexers, drop=drop)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/r2g9s5hcmndxk6lwxwavpg3ga33sf18c-python3-3.12.7-env/lib/python3.12/site-packages/xarray/core/dataset.py", line 3080, in isel
    indexes, index_variables = isel_indexes(self.xindexes, indexers)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/r2g9s5hcmndxk6lwxwavpg3ga33sf18c-python3-3.12.7-env/lib/python3.12/site-packages/xarray/core/indexes.py", line 1872, in isel_indexes
    return _apply_indexes_fast(indexes, indexers, "isel")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/r2g9s5hcmndxk6lwxwavpg3ga33sf18c-python3-3.12.7-env/lib/python3.12/site-packages/xarray/core/indexes.py", line 1827, in _apply_indexes_fast
    new_index = getattr(index, func)(index_args)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/r2g9s5hcmndxk6lwxwavpg3ga33sf18c-python3-3.12.7-env/lib/python3.12/site-packages/xarray/core/indexes.py", line 743, in isel
    return self._replace(self.index[indxr])  # type: ignore[index]
                         ~~~~~~~~~~^^^^^^^
  File "/nix/store/r2g9s5hcmndxk6lwxwavpg3ga33sf18c-python3-3.12.7-env/lib/python3.12/site-packages/pandas/core/indexes/base.py", line 5416, in __getitem__
    result = getitem(key)
             ^^^^^^^^^^^^
IndexError: boolean index did not match indexed array along dimension 0; dimension is 2 but corresponding boolean dimension is 1

However, using:

print(example_dataset.sel({"bool_dim":  True }))

Doesn't fail, but it squeezes bool_dim to a 0 size array, and I don't want that to happen, since want to end up with a bool_dim of size 1, and I don't want to use .expand_dims because I have a non-dimension coordinate attached to that bool_dim and I lose that attachment information due to #4501 .

I managed to find the following workaround using isel:

print(example_dataset.isel({
    k: (example_dataset[k].values == v).nonzero()[0]
    for k,v in loc.items()
}))

But it is not very elegant of course :).

EDIT:

Perhaps this is a duplicate of #4892 , but there no error is mentioned.

@doronbehar doronbehar added the needs triage Issue that has not been reviewed by xarray team member label Jan 1, 2025
Copy link

welcome bot commented Jan 1, 2025

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@dcherian
Copy link
Contributor

dcherian commented Jan 2, 2025

Yes can you add your nice example as a comment to #4892 please?

@dcherian dcherian closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue that has not been reviewed by xarray team member
Projects
None yet
Development

No branches or pull requests

2 participants