Skip to content

Commit

Permalink
Show list of names in error message of PandasMultiIndex.sel, update test
Browse files Browse the repository at this point in the history
  • Loading branch information
mgunyho committed Aug 19, 2023
1 parent dfe3f39 commit cff01f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion xarray/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")})
Expand Down

0 comments on commit cff01f6

Please sign in to comment.