Skip to content

Commit 513ee34

Browse files
authored
Fix Dataset.assign_coords overwriting multi-index (#7101)
1 parent e2c29f6 commit 513ee34

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

xarray/core/coordinates.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def _maybe_drop_multiindex_coords(self, coords: set[Hashable]) -> None:
315315
variables, indexes = drop_coords(
316316
coords, self._data._variables, self._data.xindexes
317317
)
318+
self._data._coord_names.intersection_update(variables)
318319
self._data._variables = variables
319320
self._data._indexes = indexes
320321

@@ -441,7 +442,7 @@ def drop_coords(
441442
f"other variables: {list(maybe_midx.index.names)!r}. "
442443
f"This will raise an error in the future. Use `.drop_vars({idx_coord_names!r})` before "
443444
"assigning new coordinate values.",
444-
DeprecationWarning,
445+
FutureWarning,
445446
stacklevel=4,
446447
)
447448
for k in idx_coord_names:

xarray/tests/test_dataarray.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,9 +1501,7 @@ def test_assign_coords(self) -> None:
15011501

15021502
def test_assign_coords_existing_multiindex(self) -> None:
15031503
data = self.mda
1504-
with pytest.warns(
1505-
DeprecationWarning, match=r"Updating MultiIndexed coordinate"
1506-
):
1504+
with pytest.warns(FutureWarning, match=r"Updating MultiIndexed coordinate"):
15071505
data.assign_coords(x=range(4))
15081506

15091507
def test_coords_alignment(self) -> None:

xarray/tests/test_dataset.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4137,16 +4137,16 @@ def test_assign_multiindex_level(self) -> None:
41374137

41384138
def test_assign_coords_existing_multiindex(self) -> None:
41394139
data = create_test_multiindex()
4140-
with pytest.warns(
4141-
DeprecationWarning, match=r"Updating MultiIndexed coordinate"
4142-
):
4140+
with pytest.warns(FutureWarning, match=r"Updating MultiIndexed coordinate"):
41434141
data.assign_coords(x=range(4))
41444142

4145-
with pytest.warns(
4146-
DeprecationWarning, match=r"Updating MultiIndexed coordinate"
4147-
):
4143+
with pytest.warns(FutureWarning, match=r"Updating MultiIndexed coordinate"):
41484144
data.assign(x=range(4))
41494145

4146+
# https://github.com/pydata/xarray/issues/7097 (coord names updated)
4147+
updated = data.assign_coords(x=range(4))
4148+
assert len(updated.coords) == 1
4149+
41504150
def test_assign_all_multiindex_coords(self) -> None:
41514151
data = create_test_multiindex()
41524152
actual = data.assign(x=range(4), level_1=range(4), level_2=range(4))

0 commit comments

Comments
 (0)