Skip to content
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

BUG: __getitem__ raise blank KeyError for IntervalIndex and missing keys #37873

Merged
merged 7 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ Indexing
- Bug in :meth:`DataFrame.loc` returned requested key plus missing values when ``loc`` was applied to single level from :class:`MultiIndex` (:issue:`27104`)
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`CategoricalIndex` using a listlike indexer containing NA values (:issue:`37722`)
- Bug in :meth:`DataFrame.xs` ignored ``droplevel=False`` for columns (:issue:`19056`)
- Bug in :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__` returns blank ``KeyError`` without missing keys for :class:`IntervalIndex` (:issue:`27365`)

Missing
^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def _convert_list_indexer(self, keyarr):

# we have missing values
if (locs == -1).any():
raise KeyError
raise KeyError(keyarr[locs == -1].tolist())

return locs

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexing/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def test_non_matching(self):

# this is a departure from our current
# indexing scheme, but simpler
with pytest.raises(KeyError, match="^$"):
with pytest.raises(KeyError, match=r"^\[-1\]$"):
s.loc[[-1, 3, 4, 5]]

with pytest.raises(KeyError, match="^$"):
with pytest.raises(KeyError, match=r"^\[-1\]$"):
s.loc[[-1, 3]]

@pytest.mark.arm_slow
Expand Down Expand Up @@ -107,11 +107,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="^$"):
with pytest.raises(KeyError, match=r"^\[10\]$"):
df.loc[[10]]

# partial missing
with pytest.raises(KeyError, match="^$"):
with pytest.raises(KeyError, match=r"^\[10\]$"):
df.loc[[10, 4]]


Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/indexing/interval/test_interval_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ def test_loc_with_overlap(self):
with pytest.raises(KeyError, match=re.escape("Interval(3, 5, closed='right')")):
s.loc[Interval(3, 5)]

with pytest.raises(KeyError, match="^$"):
with pytest.raises(KeyError, match=r"^\[Interval\(3, 5, closed='right'\)\]$"):
s.loc[[Interval(3, 5)]]

with pytest.raises(KeyError, match=re.escape("Interval(3, 5, closed='right')")):
s[Interval(3, 5)]

with pytest.raises(KeyError, match="^$"):
with pytest.raises(KeyError, match=r"^\[Interval\(3, 5, closed='right'\)\]$"):
s[[Interval(3, 5)]]

# slices with interval (only exact matches)
Expand Down Expand Up @@ -266,3 +266,11 @@ def test_non_unique_moar(self):
expected = s.iloc[[0, 1]]
result = s[[Interval(1, 3)]]
tm.assert_series_equal(expected, result)

def test_missing_key_error_message(self, frame_or_series):
# GH#27365
obj = frame_or_series(
np.arange(5), index=IntervalIndex.from_breaks(np.arange(6))
)
with pytest.raises(KeyError, match=r"\[6\]"):
obj.loc[[4, 5, 6]]