Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete associated indexes when deleting coordinate variables. #3840

Merged
merged 3 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
dcherian marked this conversation as resolved.
Show resolved Hide resolved
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
dcherian marked this conversation as resolved.
Show resolved Hide resolved
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