Skip to content

REF: remove special-casing for IntervalDtype in Index._raise_if_missing #54557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 0 additions & 12 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6159,19 +6159,7 @@ def _raise_if_missing(self, key, indexer, axis_name: str_t) -> None:
nmissing = missing_mask.sum()

if nmissing:
# TODO: remove special-case; this is just to keep exception
# message tests from raising while debugging
use_interval_msg = isinstance(self.dtype, IntervalDtype) or (
isinstance(self.dtype, CategoricalDtype)
# "Index" has no attribute "categories" [attr-defined]
and isinstance(
self.categories.dtype, IntervalDtype # type: ignore[attr-defined]
)
)

if nmissing == len(indexer):
if use_interval_msg:
key = list(key)
raise KeyError(f"None of [{key}] are in the [{axis_name}]")

not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexing/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ def test_loc_getitem_frame(self):
expected = df.take([4, 5, 4, 5])
tm.assert_frame_equal(result, expected)

with pytest.raises(KeyError, match=r"None of \[\[10\]\] are"):
msg = (
r"None of \[Index\(\[10\], dtype='object', name='B'\)\] "
r"are in the \[index\]"
)
with pytest.raises(KeyError, match=msg):
df.loc[[10]]

# partial missing
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/indexing/interval/test_interval_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ def test_loc_with_overlap(self, indexer_sl):
with pytest.raises(KeyError, match=re.escape("Interval(3, 5, closed='right')")):
indexer_sl(ser)[Interval(3, 5)]

msg = r"None of \[\[Interval\(3, 5, closed='right'\)\]\]"
msg = (
r"None of \[IntervalIndex\(\[\(3, 5\]\], "
r"dtype='interval\[int64, right\]'\)\] are in the \[index\]"
)
with pytest.raises(KeyError, match=msg):
indexer_sl(ser)[[Interval(3, 5)]]

Expand Down