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

isel: convert IndexVariable to Variable if index is dropped #6388

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,8 @@ def _isel_fancy(
new_var = var.isel(indexers=var_indexers)
else:
new_var = var.copy(deep=False)
if name not in indexes:
new_var = new_var.to_base_variable()
variables[name] = new_var

coord_names = self._coord_names & variables.keys()
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,15 @@ def test_isel_dataarray(self):
with pytest.raises(IndexError, match=r"dimension coordinate 'dim2'"):
actual = data.isel(dim2=indexing_ds["dim2"])

def test_isel_fancy_convert_index_variable(self) -> None:
# select index variable "x" with a DataArray of dim "z"
# -> drop index and convert index variable to base variable
ds = xr.Dataset({"foo": ("x", [1, 2, 3])}, coords={"x": [0, 1, 2]})
idxr = xr.DataArray([1], dims="z", name="x")
actual = ds.isel(x=idxr)
assert "x" not in actual.xindexes
assert not isinstance(actual.x.variable, IndexVariable)

def test_sel(self):
data = create_test_data()
int_slicers = {"dim1": slice(None, None, 2), "dim2": slice(2), "dim3": slice(3)}
Expand Down