Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,14 @@ def sel(
f"not all values found in index {coord_name!r}"
)
else:
indexer = self.index.get_loc(label_value)
try:
indexer = self.index.get_loc(label_value)
except KeyError as e:
raise KeyError(
f"not all values found in index {coord_name!r}. "
"Did you mean to set `method=`?"
) from e

elif label_array.dtype.kind == "b":
indexer = label_array
else:
Expand Down
3 changes: 3 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,9 @@ def test_sel_no_index(self) -> None:
def test_sel_method(self) -> None:
data = DataArray(np.random.randn(3, 4), [("x", [0, 1, 2]), ("y", list("abcd"))])

with pytest.raises(KeyError, match="Did you mean to set `method=`?"):
data.sel(y="ab")

expected = data.sel(y=["a", "b"])
actual = data.sel(y=["ab", "ba"], method="pad")
assert_identical(expected, actual)
Expand Down