Skip to content

Commit 5354679

Browse files
authored
Delete associated indexes when deleting coordinate variables. (#3840)
* Delete associated indexes when deleting coordinate variables. Fixes #3746 * review * fix tests
1 parent 889240b commit 5354679

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

doc/whats-new.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ Bug fixes
9191
to preserve attributes. :py:meth:`Dataset.coarsen` accepts a keyword
9292
argument ``keep_attrs`` to change this setting. (:issue:`3376`,
9393
:pull:`3801`) By `Andrew Thomas <https://github.com/amcnicho>`_.
94-
94+
- Delete associated indexes when deleting coordinate variables. (:issue:`3746`).
95+
By `Deepak Cherian <https://github.com/dcherian>`_.
9596
- Fix :py:meth:`xarray.core.dataset.Dataset.to_zarr` when using `append_dim` and `group`
9697
simultaneously. (:issue:`3170`). By `Matthias Meyer <https://github.com/niowniow>`_.
9798
- Fix html repr on :py:class:`Dataset` with non-string keys (:pull:`3807`).

xarray/core/coordinates.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def __delitem__(self, key: Hashable) -> None:
247247
if key in self:
248248
del self._data[key]
249249
else:
250-
raise KeyError(key)
250+
raise KeyError(f"{key!r} is not a coordinate variable.")
251251

252252
def _ipython_key_completions_(self):
253253
"""Provide method for the key-autocompletions in IPython. """
@@ -291,7 +291,7 @@ def _update_coords(
291291
dims = calculate_dimensions(coords_plus_data)
292292
if not set(dims) <= set(self.dims):
293293
raise ValueError(
294-
"cannot add coordinates with new dimensions to " "a DataArray"
294+
"cannot add coordinates with new dimensions to a DataArray"
295295
)
296296
self._data._coords = coords
297297

@@ -312,7 +312,12 @@ def to_dataset(self) -> "Dataset":
312312
return Dataset._construct_direct(coords, set(coords))
313313

314314
def __delitem__(self, key: Hashable) -> None:
315-
del self._data._coords[key]
315+
if key in self:
316+
del self._data._coords[key]
317+
if self._data._indexes is not None and key in self._data._indexes:
318+
del self._data._indexes[key]
319+
else:
320+
raise KeyError(f"{key!r} is not a coordinate variable.")
316321

317322
def _ipython_key_completions_(self):
318323
"""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)