Skip to content
forked from pydata/xarray

Commit 539338e

Browse files
committed
Delete associated indexes when deleting coordinate variables.
Fixes pydata#3746
1 parent 01462d6 commit 539338e

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

doc/whats-new.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ Bug fixes
6565
to preserve attributes. :py:meth:`Dataset.coarsen` accepts a keyword
6666
argument ``keep_attrs`` to change this setting. (:issue:`3376`,
6767
:pull:`3801`) By `Andrew Thomas <https://github.com/amcnicho>`_.
68-
68+
- Delete associated indexes when deleting coordinate variables. (:issue:`3746`).
69+
By `Deepak Cherian <https://github.com/dcherian>`_.
6970
- Fix :py:meth:`xarray.core.dataset.Dataset.to_zarr` when using `append_dim` and `group`
7071
simultaneously. (:issue:`3170`). By `Matthias Meyer <https://github.com/niowniow>`_.
7172

xarray/core/coordinates.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,12 @@ def _update_coords(
246246
def __delitem__(self, key: Hashable) -> None:
247247
if key in self:
248248
del self._data[key]
249+
if key in self._data.indexes:
250+
indexes = dict(self._data.indexes)
251+
indexes.pop(key)
252+
self._data._indexes = indexes
249253
else:
250-
raise KeyError(key)
254+
raise KeyError(f"{key!r} is not a coordinate variable.")
251255

252256
def _ipython_key_completions_(self):
253257
"""Provide method for the key-autocompletions in IPython. """
@@ -291,7 +295,7 @@ def _update_coords(
291295
dims = calculate_dimensions(coords_plus_data)
292296
if not set(dims) <= set(self.dims):
293297
raise ValueError(
294-
"cannot add coordinates with new dimensions to " "a DataArray"
298+
"cannot add coordinates with new dimensions to a DataArray"
295299
)
296300
self._data._coords = coords
297301

@@ -312,7 +316,14 @@ def to_dataset(self) -> "Dataset":
312316
return Dataset._construct_direct(coords, set(coords))
313317

314318
def __delitem__(self, key: Hashable) -> None:
315-
del self._data._coords[key]
319+
if key in self:
320+
del self._data._coords[key]
321+
if key in self._data.indexes:
322+
indexes = dict(self._data.indexes)
323+
indexes.pop(key)
324+
self._data._indexes = indexes
325+
else:
326+
raise KeyError(f"{key!r} is not a coordinate variable.")
316327

317328
def _ipython_key_completions_(self):
318329
"""Provide method for the key-autocompletions in IPython. """

xarray/tests/test_dataarray.py

+6
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,12 @@ def test_coords_non_string(self):
14121412
expected = DataArray(2, coords={1: 2}, name=1)
14131413
assert_identical(actual, expected)
14141414

1415+
def test_coords_delitem_delete_indexes(self):
1416+
# regression test for GH3746
1417+
arr = DataArray(np.ones((2,)), dims="x", coords={"x": [0, 1]})
1418+
del arr.coords["x"]
1419+
assert "x" not in arr.indexes
1420+
14151421
def test_broadcast_like(self):
14161422
arr1 = DataArray(
14171423
np.ones((2, 3)),

xarray/tests/test_dataset.py

+4
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,10 @@ def test_coords_modify(self):
744744
expected = data.merge({"c": 11}).set_coords("c")
745745
assert_identical(expected, actual)
746746

747+
# regression test for GH3746
748+
del actual.coords["x"]
749+
assert "x" not in actual.indexes
750+
747751
def test_update_index(self):
748752
actual = Dataset(coords={"x": [1, 2, 3]})
749753
actual["x"] = ["a", "b", "c"]

0 commit comments

Comments
 (0)