From cff01f613a2e15926f7b97e31bd8a7411a86c919 Mon Sep 17 00:00:00 2001 From: mgunyho <20118130+mgunyho@users.noreply.github.com> Date: Sat, 19 Aug 2023 13:31:23 +0300 Subject: [PATCH] Show list of names in error message of PandasMultiIndex.sel, update test --- xarray/core/indexes.py | 6 +++--- xarray/tests/test_indexes.py | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index 7de290f4e14..b42046e1706 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -1196,12 +1196,12 @@ def sel(self, labels, method=None, tolerance=None) -> IndexSelResult: coord_name, label = next(iter(labels.items())) if is_dict_like(label): - invalid_levels = [ + invalid_levels = tuple( name for name in label if name not in self.index.names - ] + ) if invalid_levels: raise ValueError( - f"invalid multi-index level names {invalid_levels}" + f"multi-index level names {invalid_levels} not found in indexes {tuple(self.index.names)}" ) return self.sel(label) diff --git a/xarray/tests/test_indexes.py b/xarray/tests/test_indexes.py index ebe9f3fb932..4ba849ea3ed 100644 --- a/xarray/tests/test_indexes.py +++ b/xarray/tests/test_indexes.py @@ -482,7 +482,10 @@ def test_sel(self) -> None: index.sel({"x": 0}) with pytest.raises(ValueError, match=r"cannot provide labels for both.*"): index.sel({"one": 0, "x": "a"}) - with pytest.raises(ValueError, match=r"invalid multi-index level names"): + with pytest.raises( + ValueError, + match=r"multi-index level names \('three',\) not found in indexes", + ): index.sel({"x": {"three": 0}}) with pytest.raises(IndexError): index.sel({"x": (slice(None), 1, "no_level")})