From 914a1a17832446fdecf928cd704f8751b4533e46 Mon Sep 17 00:00:00 2001 From: Benoit Bovy Date: Wed, 28 Sep 2022 18:11:34 +0200 Subject: [PATCH 1/2] fix assign_coords multi-index: update coord names --- xarray/core/coordinates.py | 1 + xarray/tests/test_dataset.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/xarray/core/coordinates.py b/xarray/core/coordinates.py index 42cc8130810..86bc2fccbdc 100644 --- a/xarray/core/coordinates.py +++ b/xarray/core/coordinates.py @@ -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 diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index bc6410a6d4a..3e6049a475b 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4146,6 +4146,10 @@ def test_assign_coords_existing_multiindex(self) -> None: ): 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)) From ee9b027c0e41de15fc4960dde9e4c551d7d2a9df Mon Sep 17 00:00:00 2001 From: Benoit Bovy Date: Wed, 28 Sep 2022 18:13:50 +0200 Subject: [PATCH 2/2] DeprecationWarning -> FutureWarning The `DeprecationWarning` is ignored by default for `.assign_coords` due to the stacklevel (https://github.com/pydata/xarray/pull/6798#discussion_r924653224) Use `FutureWarning` instead to show the warning for both `.assign` and `.assign_coords`. --- xarray/core/coordinates.py | 2 +- xarray/tests/test_dataarray.py | 4 +--- xarray/tests/test_dataset.py | 8 ++------ 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/xarray/core/coordinates.py b/xarray/core/coordinates.py index 86bc2fccbdc..47350b9403f 100644 --- a/xarray/core/coordinates.py +++ b/xarray/core/coordinates.py @@ -442,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: diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 3602b87102d..bdbf2deac12 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -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: diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 3e6049a475b..07ac491a10c 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4136,14 +4136,10 @@ 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)