Skip to content
forked from pydata/xarray

Commit

Permalink
Delete associated indexes when deleting coordinate variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Mar 6, 2020
1 parent 01462d6 commit 539338e
Show file tree
Hide file tree
Showing 4 changed files with 26 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 @@ -65,7 +65,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>`_.

Expand Down
17 changes: 14 additions & 3 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,12 @@ def _update_coords(
def __delitem__(self, key: Hashable) -> None:
if key in self:
del self._data[key]
if key in self._data.indexes:
indexes = dict(self._data.indexes)
indexes.pop(key)
self._data._indexes = indexes
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 +295,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 +316,14 @@ 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 key in self._data.indexes:
indexes = dict(self._data.indexes)
indexes.pop(key)
self._data._indexes = indexes
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 539338e

Please sign in to comment.