From 4a0bb2eb80538806468233d11bc5a4c06ffb417e Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Wed, 20 Mar 2024 09:00:18 -0600 Subject: [PATCH] pandas 3 MultiIndex fixes (#8847) * Fix dropping of muiltiindexes xref #8844 Closes https://github.com/xarray-contrib/flox/issues/342 * More fixes --- xarray/core/dataset.py | 2 +- xarray/tests/test_indexes.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index b4c00b66ed8..10bf1466156 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -5867,7 +5867,7 @@ def drop_vars( for var in names_set: maybe_midx = self._indexes.get(var, None) if isinstance(maybe_midx, PandasMultiIndex): - idx_coord_names = set(maybe_midx.index.names + [maybe_midx.dim]) + idx_coord_names = set(list(maybe_midx.index.names) + [maybe_midx.dim]) idx_other_names = idx_coord_names - set(names_set) other_names.update(idx_other_names) if other_names: diff --git a/xarray/tests/test_indexes.py b/xarray/tests/test_indexes.py index 3ee7f045360..5ebdfd5da6e 100644 --- a/xarray/tests/test_indexes.py +++ b/xarray/tests/test_indexes.py @@ -352,7 +352,7 @@ def test_constructor(self) -> None: # default level names pd_idx = pd.MultiIndex.from_arrays([foo_data, bar_data]) index = PandasMultiIndex(pd_idx, "x") - assert index.index.names == ("x_level_0", "x_level_1") + assert list(index.index.names) == ["x_level_0", "x_level_1"] def test_from_variables(self) -> None: v_level1 = xr.Variable( @@ -370,7 +370,7 @@ def test_from_variables(self) -> None: assert index.dim == "x" assert index.index.equals(expected_idx) assert index.index.name == "x" - assert index.index.names == ["level1", "level2"] + assert list(index.index.names) == ["level1", "level2"] var = xr.Variable(("x", "y"), [[1, 2, 3], [4, 5, 6]]) with pytest.raises( @@ -413,7 +413,8 @@ def test_stack(self) -> None: index = PandasMultiIndex.stack(prod_vars, "z") assert index.dim == "z" - assert index.index.names == ["x", "y"] + # TODO: change to tuple when pandas 3 is minimum + assert list(index.index.names) == ["x", "y"] np.testing.assert_array_equal( index.index.codes, [[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]] ) @@ -531,12 +532,12 @@ def test_rename(self) -> None: assert new_index is index new_index = index.rename({"two": "three"}, {}) - assert new_index.index.names == ["one", "three"] + assert list(new_index.index.names) == ["one", "three"] assert new_index.dim == "x" assert new_index.level_coords_dtype == {"one": "