From 142cee3aaaed1fd1a678b4af26157fec1fb6670b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 18 Apr 2020 19:16:45 -0700 Subject: [PATCH 1/4] BUG/API: getitem behavior with list match ndarray/index/series --- pandas/core/series.py | 7 ++----- pandas/tests/series/indexing/test_boolean.py | 5 ----- pandas/tests/series/indexing/test_getitem.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 9182e378fbaeb..256c6959fe6fb 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -949,11 +949,8 @@ def _get_with(self, key): else: return self.iloc[key] - if isinstance(key, list): - # handle the dup indexing case GH#4246 - return self.loc[key] - - return self.reindex(key) + # handle the dup indexing case GH#4246 + return self.loc[key] def _get_values_tuple(self, key): # mpl hackaround diff --git a/pandas/tests/series/indexing/test_boolean.py b/pandas/tests/series/indexing/test_boolean.py index 8878a4a6526af..e2b71b1f2f412 100644 --- a/pandas/tests/series/indexing/test_boolean.py +++ b/pandas/tests/series/indexing/test_boolean.py @@ -28,11 +28,6 @@ def test_getitem_boolean_empty(): # GH5877 # indexing with empty series - s = Series(["A", "B"]) - expected = Series(np.nan, index=["C"], dtype=object) - result = s[Series(["C"], dtype=object)] - tm.assert_series_equal(result, expected) - s = Series(["A", "B"]) expected = Series(dtype=object, index=Index([], dtype="int64")) result = s[Series([], dtype=object)] diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 2922f3c741320..1f26982291495 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -78,6 +78,18 @@ def test_getitem_median_slice_bug(self): class TestSeriesGetitemListLike: + @pytest.mark.parametrize("box", [list, np.array, pd.Index, pd.Series]) + def test_getitem_no_matches(self, box): + # GH#???? we expect the same behavior for list/ndarray/Index/Serioes + ser = Series(["A", "B"]) + + key = Series(["C"], dtype=object) + key = box(key) + + msg = r"None of \[Index\(\['C'\], dtype='object'\)\] are in the \[index\]" + with pytest.raises(KeyError, match=msg): + ser[key] + def test_getitem_intlist_intindex_periodvalues(self): ser = Series(period_range("2000-01-01", periods=10, freq="D")) From a8b48eea6f26bea7b057c668749ecc594099bc94 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 18 Apr 2020 19:18:27 -0700 Subject: [PATCH 2/4] GH ref --- pandas/tests/series/indexing/test_getitem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 1f26982291495..25887d785f489 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -80,7 +80,7 @@ def test_getitem_median_slice_bug(self): class TestSeriesGetitemListLike: @pytest.mark.parametrize("box", [list, np.array, pd.Index, pd.Series]) def test_getitem_no_matches(self, box): - # GH#???? we expect the same behavior for list/ndarray/Index/Serioes + # GH#33462 we expect the same behavior for list/ndarray/Index/Serioes ser = Series(["A", "B"]) key = Series(["C"], dtype=object) From 951ed22e739d445411cd9006b78fad2609b370b5 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 19 Apr 2020 11:19:26 -0700 Subject: [PATCH 3/4] typo fixup --- pandas/tests/series/indexing/test_getitem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 25887d785f489..9ce31f5f6decf 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -80,7 +80,7 @@ def test_getitem_median_slice_bug(self): class TestSeriesGetitemListLike: @pytest.mark.parametrize("box", [list, np.array, pd.Index, pd.Series]) def test_getitem_no_matches(self, box): - # GH#33462 we expect the same behavior for list/ndarray/Index/Serioes + # GH#33462 we expect the same behavior for list/ndarray/Index/Series ser = Series(["A", "B"]) key = Series(["C"], dtype=object) From 3fd6382036d04fafce290ef4d57ae389a55e1fb1 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 19 Apr 2020 11:20:56 -0700 Subject: [PATCH 4/4] whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 03a547fadd7ca..9d40f9b6ffa2c 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -529,6 +529,7 @@ Indexing - 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`) - 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`) - Bug in :meth:`Series.__setitem__` with an :class:`IntervalIndex` and a list-like key of integers (:issue:`33473`) +- 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`) Missing ^^^^^^^