Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 11041bd

Browse filesBrowse files
dcherianbenbovy
andauthored
Restore old MultiIndex dropping behaviour (pydata#6592)
Co-authored-by: Benoit Bovy <bbovy@gfz-potsdam.de>
1 parent f6d0f84 commit 11041bd
Copy full SHA for 11041bd

3 files changed

+26-9Lines changed: 26 additions & 9 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎xarray/core/dataset.py‎

Copy file name to clipboardExpand all lines: xarray/core/dataset.py
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4551,6 +4551,23 @@ def drop_vars(
45514551
if errors == "raise":
45524552
self._assert_all_in_dataset(names)
45534553

4554+
# GH6505
4555+
other_names = set()
4556+
for var in names:
4557+
maybe_midx = self._indexes.get(var, None)
4558+
if isinstance(maybe_midx, PandasMultiIndex):
4559+
idx_coord_names = set(maybe_midx.index.names + [maybe_midx.dim])
4560+
idx_other_names = idx_coord_names - set(names)
4561+
other_names.update(idx_other_names)
4562+
if other_names:
4563+
names |= set(other_names)
4564+
warnings.warn(
4565+
f"Deleting a single level of a MultiIndex is deprecated. Previously, this deleted all levels of a MultiIndex. "
4566+
f"Please also drop the following variables: {other_names!r} to avoid an error in the future.",
4567+
DeprecationWarning,
4568+
stacklevel=2,
4569+
)
4570+
45544571
assert_no_index_corrupted(self.xindexes, names)
45554572

45564573
variables = {k: v for k, v in self._variables.items() if k not in names}
Collapse file

‎xarray/tests/test_dataarray.py‎

Copy file name to clipboardExpand all lines: xarray/tests/test_dataarray.py
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,10 +2360,11 @@ def test_drop_coordinates(self):
23602360
assert_identical(actual, renamed)
23612361

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

23682369
def test_drop_all_multiindex_levels(self):
23692370
dim_levels = ["x", "level_1", "level_2"]
Collapse file

‎xarray/tests/test_dataset.py‎

Copy file name to clipboardExpand all lines: xarray/tests/test_dataset.py
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,11 +2453,10 @@ def test_drop_variables(self):
24532453

24542454
def test_drop_multiindex_level(self):
24552455
data = create_test_multiindex()
2456-
2457-
with pytest.raises(
2458-
ValueError, match=r"cannot remove coordinate.*corrupt.*index "
2459-
):
2460-
data.drop_vars("level_1")
2456+
expected = data.drop_vars(["x", "level_1", "level_2"])
2457+
with pytest.warns(DeprecationWarning):
2458+
actual = data.drop_vars("level_1")
2459+
assert_identical(expected, actual)
24612460

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.