Skip to content

Commit

Permalink
isel: convert IndexVariable to Variable if index is dropped (#6388)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy authored Mar 21, 2022
1 parent fed8520 commit 067b2e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
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

0 comments on commit 067b2e8

Please sign in to comment.