Skip to content

Fix Dataset.assign_coords overwriting multi-index #7101

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

Merged
merged 2 commits into from
Sep 28, 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
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 @@ -4136,16 +4136,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