Skip to content

Commit 56d0934

Browse files
authored
BUG/API: getitem behavior with list match ndarray/index/series (#33646)
1 parent 4071c3b commit 56d0934

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ Indexing
529529
- Bug in :meth:`DataFrame.iloc` when slicing a single column-:class:`DataFrame`` with ``ExtensionDtype`` (e.g. ``df.iloc[:, :1]``) returning an invalid result (:issue:`32957`)
530530
- Bug in :meth:`DatetimeIndex.insert` and :meth:`TimedeltaIndex.insert` causing index ``freq`` to be lost when setting an element into an empty :class:`Series` (:issue:33573`)
531531
- Bug in :meth:`Series.__setitem__` with an :class:`IntervalIndex` and a list-like key of integers (:issue:`33473`)
532+
- Bug in :meth:`Series.__getitem__` allowing missing labels with ``np.ndarray``, :class:`Index`, :class:`Series` indexers but not ``list``, these now all raise ``KeyError`` (:issue:`33646`)
532533

533534
Missing
534535
^^^^^^^

pandas/core/series.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -949,11 +949,8 @@ def _get_with(self, key):
949949
else:
950950
return self.iloc[key]
951951

952-
if isinstance(key, list):
953-
# handle the dup indexing case GH#4246
954-
return self.loc[key]
955-
956-
return self.reindex(key)
952+
# handle the dup indexing case GH#4246
953+
return self.loc[key]
957954

958955
def _get_values_tuple(self, key):
959956
# mpl hackaround

pandas/tests/series/indexing/test_boolean.py

-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ def test_getitem_boolean_empty():
2828

2929
# GH5877
3030
# indexing with empty series
31-
s = Series(["A", "B"])
32-
expected = Series(np.nan, index=["C"], dtype=object)
33-
result = s[Series(["C"], dtype=object)]
34-
tm.assert_series_equal(result, expected)
35-
3631
s = Series(["A", "B"])
3732
expected = Series(dtype=object, index=Index([], dtype="int64"))
3833
result = s[Series([], dtype=object)]

pandas/tests/series/indexing/test_getitem.py

+12
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ def test_getitem_median_slice_bug(self):
7878

7979

8080
class TestSeriesGetitemListLike:
81+
@pytest.mark.parametrize("box", [list, np.array, pd.Index, pd.Series])
82+
def test_getitem_no_matches(self, box):
83+
# GH#33462 we expect the same behavior for list/ndarray/Index/Series
84+
ser = Series(["A", "B"])
85+
86+
key = Series(["C"], dtype=object)
87+
key = box(key)
88+
89+
msg = r"None of \[Index\(\['C'\], dtype='object'\)\] are in the \[index\]"
90+
with pytest.raises(KeyError, match=msg):
91+
ser[key]
92+
8193
def test_getitem_intlist_intindex_periodvalues(self):
8294
ser = Series(period_range("2000-01-01", periods=10, freq="D"))
8395

0 commit comments

Comments
 (0)