Skip to content

Commit

Permalink
Fix Dataset.assign_coords overwriting multi-index (#7101)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy authored Sep 28, 2022
1 parent e2c29f6 commit 513ee34
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
3 changes: 2 additions & 1 deletion xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def _maybe_drop_multiindex_coords(self, coords: set[Hashable]) -> None:
variables, indexes = drop_coords(
coords, self._data._variables, self._data.xindexes
)
self._data._coord_names.intersection_update(variables)
self._data._variables = variables
self._data._indexes = indexes

Expand Down Expand Up @@ -441,7 +442,7 @@ def drop_coords(
f"other variables: {list(maybe_midx.index.names)!r}. "
f"This will raise an error in the future. Use `.drop_vars({idx_coord_names!r})` before "
"assigning new coordinate values.",
DeprecationWarning,
FutureWarning,
stacklevel=4,
)
for k in idx_coord_names:
Expand Down
4 changes: 1 addition & 3 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,9 +1501,7 @@ def test_assign_coords(self) -> None:

def test_assign_coords_existing_multiindex(self) -> None:
data = self.mda
with pytest.warns(
DeprecationWarning, match=r"Updating MultiIndexed coordinate"
):
with pytest.warns(FutureWarning, match=r"Updating MultiIndexed coordinate"):
data.assign_coords(x=range(4))

def test_coords_alignment(self) -> None:
Expand Down
12 changes: 6 additions & 6 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4137,16 +4137,16 @@ def test_assign_multiindex_level(self) -> None:

def test_assign_coords_existing_multiindex(self) -> None:
data = create_test_multiindex()
with pytest.warns(
DeprecationWarning, match=r"Updating MultiIndexed coordinate"
):
with pytest.warns(FutureWarning, match=r"Updating MultiIndexed coordinate"):
data.assign_coords(x=range(4))

with pytest.warns(
DeprecationWarning, match=r"Updating MultiIndexed coordinate"
):
with pytest.warns(FutureWarning, match=r"Updating MultiIndexed coordinate"):
data.assign(x=range(4))

# https://github.com/pydata/xarray/issues/7097 (coord names updated)
updated = data.assign_coords(x=range(4))
assert len(updated.coords) == 1

def test_assign_all_multiindex_coords(self) -> None:
data = create_test_multiindex()
actual = data.assign(x=range(4), level_1=range(4), level_2=range(4))
Expand Down

0 comments on commit 513ee34

Please sign in to comment.