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

Restore old MultiIndex dropping behaviour #6592

Merged
merged 5 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4551,6 +4551,23 @@ def drop_vars(
if errors == "raise":
self._assert_all_in_dataset(names)

# GH6505
other_names = set()
for var in names:
maybe_midx = self._indexes.get(var, None)
if isinstance(maybe_midx, PandasMultiIndex):
idx_coord_names = set(maybe_midx.index.names + [maybe_midx.dim])
idx_other_names = idx_coord_names - set(names)
other_names.update(idx_other_names)
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,
dcherian marked this conversation as resolved.
Show resolved Hide resolved
stacklevel=2,
)

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
9 changes: 4 additions & 5 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2418,11 +2418,10 @@ def test_drop_variables(self):

def test_drop_multiindex_level(self):
data = create_test_multiindex()

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

def test_drop_index_labels(self):
data = Dataset({"A": (["x", "y"], np.random.randn(2, 3)), "x": ["a", "b"]})
Expand Down