Skip to content

Commit 2863428

Browse files
authored
BUG: __getitem__ raise blank KeyError for IntervalIndex and missing keys (#37873)
1 parent d8a2e74 commit 2863428

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ Indexing
627627
- Bug in :meth:`DataFrame.loc` raising ``TypeError`` when non-integer slice was given to select values from :class:`MultiIndex` (:issue:`25165`, :issue:`24263`)
628628
- Bug in :meth:`DataFrame.loc` returning and assigning elements in wrong order when indexer is differently ordered than the :class:`MultiIndex` to filter (:issue:`31330`, :issue:`34603`)
629629
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.__getitem__` raising ``KeyError`` when columns were :class:`MultiIndex` with only one level (:issue:`29749`)
630+
- Bug in :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__` raising blank ``KeyError`` without missing keys for :class:`IntervalIndex` (:issue:`27365`)
630631

631632
Missing
632633
^^^^^^^

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ def _convert_list_indexer(self, keyarr):
806806

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

811811
return locs
812812

pandas/tests/indexing/interval/test_interval.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def test_non_matching(self):
6565

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

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

7474
@pytest.mark.arm_slow
@@ -107,11 +107,11 @@ def test_loc_getitem_frame(self):
107107
expected = df.take([4, 5, 4, 5])
108108
tm.assert_frame_equal(result, expected)
109109

110-
with pytest.raises(KeyError, match="^$"):
110+
with pytest.raises(KeyError, match=r"^\[10\]$"):
111111
df.loc[[10]]
112112

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

117117

pandas/tests/indexing/interval/test_interval_new.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,13 @@ def test_loc_with_overlap(self):
204204
with pytest.raises(KeyError, match=re.escape("Interval(3, 5, closed='right')")):
205205
s.loc[Interval(3, 5)]
206206

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

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

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

216216
# slices with interval (only exact matches)
@@ -266,3 +266,11 @@ def test_non_unique_moar(self):
266266
expected = s.iloc[[0, 1]]
267267
result = s[[Interval(1, 3)]]
268268
tm.assert_series_equal(expected, result)
269+
270+
def test_missing_key_error_message(self, frame_or_series):
271+
# GH#27365
272+
obj = frame_or_series(
273+
np.arange(5), index=IntervalIndex.from_breaks(np.arange(6))
274+
)
275+
with pytest.raises(KeyError, match=r"\[6\]"):
276+
obj.loc[[4, 5, 6]]

0 commit comments

Comments
 (0)