Skip to content

Commit 067b2e8

Browse files
authored
isel: convert IndexVariable to Variable if index is dropped (#6388)
1 parent fed8520 commit 067b2e8

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

xarray/core/dataset.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,8 @@ def _isel_fancy(
22622262
new_var = var.isel(indexers=var_indexers)
22632263
else:
22642264
new_var = var.copy(deep=False)
2265+
if name not in indexes:
2266+
new_var = new_var.to_base_variable()
22652267
variables[name] = new_var
22662268

22672269
coord_names = self._coord_names & variables.keys()

xarray/tests/test_dataset.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,15 @@ def test_isel_dataarray(self):
12621262
with pytest.raises(IndexError, match=r"dimension coordinate 'dim2'"):
12631263
actual = data.isel(dim2=indexing_ds["dim2"])
12641264

1265+
def test_isel_fancy_convert_index_variable(self) -> None:
1266+
# select index variable "x" with a DataArray of dim "z"
1267+
# -> drop index and convert index variable to base variable
1268+
ds = xr.Dataset({"foo": ("x", [1, 2, 3])}, coords={"x": [0, 1, 2]})
1269+
idxr = xr.DataArray([1], dims="z", name="x")
1270+
actual = ds.isel(x=idxr)
1271+
assert "x" not in actual.xindexes
1272+
assert not isinstance(actual.x.variable, IndexVariable)
1273+
12651274
def test_sel(self):
12661275
data = create_test_data()
12671276
int_slicers = {"dim1": slice(None, None, 2), "dim2": slice(2), "dim3": slice(3)}

0 commit comments

Comments
 (0)