Skip to content
forked from pydata/xarray

Commit

Permalink
Restore old MultiIndex dropping behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed May 11, 2022
1 parent 770e878 commit b0022cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
16 changes: 16 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4551,6 +4551,22 @@ def drop_vars(
if errors == "raise":
self._assert_all_in_dataset(names)

# GH6505
other_names = []
for var in names:
maybe_midx = self._indexes.get(var, None)
if isinstance(maybe_midx, PandasMultiIndex):
for other in set(self._indexes) - set(names):
if self._indexes[other].equals(maybe_midx):
other_names.append(other)
if other_names:
names |= set(other_names)
warnings.warn(
f"Deleting a single level of a MultiIndex is deprecated. Previously, this deleted all levels of a MultiIndex. "
f"Please also drop the following variables: {other_names!r} to avoid an error in the future.",
DeprecationWarning,
)

assert_no_index_corrupted(self.xindexes, names)

variables = {k: v for k, v in self._variables.items() if k not in names}
Expand Down
9 changes: 5 additions & 4 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2360,10 +2360,11 @@ def test_drop_coordinates(self):
assert_identical(actual, renamed)

def test_drop_multiindex_level(self):
with pytest.raises(
ValueError, match=r"cannot remove coordinate.*corrupt.*index "
):
self.mda.drop_vars("level_1")
# GH6505
expected = self.mda.drop_vars(["x", "level_1", "level_2"])
with pytest.warns(DeprecationWarning):
actual = self.mda.drop_vars("level_1")
assert_identical(expected, actual)

def test_drop_all_multiindex_levels(self):
dim_levels = ["x", "level_1", "level_2"]
Expand Down

0 comments on commit b0022cb

Please sign in to comment.