Skip to content

Commit

Permalink
Delete associated indexes when deleting coordinate variables. (#3840)
Browse files Browse the repository at this point in the history
* Delete associated indexes when deleting coordinate variables.

Fixes #3746

* review

* fix tests
  • Loading branch information
dcherian authored Mar 21, 2020
1 parent 889240b commit 5354679
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ Bug fixes
to preserve attributes. :py:meth:`Dataset.coarsen` accepts a keyword
argument ``keep_attrs`` to change this setting. (:issue:`3376`,
:pull:`3801`) By `Andrew Thomas <https://github.com/amcnicho>`_.

- Delete associated indexes when deleting coordinate variables. (:issue:`3746`).
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix :py:meth:`xarray.core.dataset.Dataset.to_zarr` when using `append_dim` and `group`
simultaneously. (:issue:`3170`). By `Matthias Meyer <https://github.com/niowniow>`_.
- Fix html repr on :py:class:`Dataset` with non-string keys (:pull:`3807`).
Expand Down
11 changes: 8 additions & 3 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def __delitem__(self, key: Hashable) -> None:
if key in self:
del self._data[key]
else:
raise KeyError(key)
raise KeyError(f"{key!r} is not a coordinate variable.")

def _ipython_key_completions_(self):
"""Provide method for the key-autocompletions in IPython. """
Expand Down Expand Up @@ -291,7 +291,7 @@ def _update_coords(
dims = calculate_dimensions(coords_plus_data)
if not set(dims) <= set(self.dims):
raise ValueError(
"cannot add coordinates with new dimensions to " "a DataArray"
"cannot add coordinates with new dimensions to a DataArray"
)
self._data._coords = coords

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

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

def _ipython_key_completions_(self):
"""Provide method for the key-autocompletions in IPython. """
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,12 @@ def test_coords_non_string(self):
expected = DataArray(2, coords={1: 2}, name=1)
assert_identical(actual, expected)

def test_coords_delitem_delete_indexes(self):
# regression test for GH3746
arr = DataArray(np.ones((2,)), dims="x", coords={"x": [0, 1]})
del arr.coords["x"]
assert "x" not in arr.indexes

def test_broadcast_like(self):
arr1 = DataArray(
np.ones((2, 3)),
Expand Down
4 changes: 4 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ def test_coords_modify(self):
expected = data.merge({"c": 11}).set_coords("c")
assert_identical(expected, actual)

# regression test for GH3746
del actual.coords["x"]
assert "x" not in actual.indexes

def test_update_index(self):
actual = Dataset(coords={"x": [1, 2, 3]})
actual["x"] = ["a", "b", "c"]
Expand Down

0 comments on commit 5354679

Please sign in to comment.