From e6ea4da6e56f306e0aefa4e81f64ed5b9e7413a0 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 18 Nov 2019 15:53:24 -0800 Subject: [PATCH 1/3] BUG: Index.get_loc raising incorrect error, closes #29189 --- pandas/_libs/index.pyx | 8 ++++++-- pandas/tests/groupby/test_groupby.py | 10 ++++++++++ pandas/tests/indexing/test_indexing.py | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 92937ae56817c..2c69d6aaaf950 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -141,8 +141,12 @@ cdef class IndexEngine: if self.is_monotonic_increasing: values = self._get_index_values() - left = values.searchsorted(val, side='left') - right = values.searchsorted(val, side='right') + try: + left = values.searchsorted(val, side='left') + right = values.searchsorted(val, side='right') + except TypeError: + # e.g. GH#29189 get_loc(None) with a Float64Index + raise KeyError(val) diff = right - left if diff == 0: diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index e17181f55fdba..d175d7fb1072b 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -1951,3 +1951,13 @@ def test_groupby_only_none_group(): expected = pd.Series([np.nan], name="x") tm.assert_series_equal(actual, expected) + + +def test_groupby_duplicate_index(): + # GH#29189 the groupby call here used to raise + ser = pd.Series([2, 5, 6, 8], index=[2.0, 4.0, 4.0, 5.0]) + gb = ser.groupby(level=0) + + result = gb.mean() + expected = pd.Series([2, 5.5, 8], index=[2., 4., 5.]) + tm.assert_series_equal(result, expected) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index fc5753ec2955c..ea9bc91a13111 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -1209,3 +1209,16 @@ def test_1tuple_without_multiindex(): result = ser[key] expected = ser[key[0]] tm.assert_series_equal(result, expected) + + +def test_duplicate_index_mistyped_key_raises_keyerror(): + # GH#29189 float_index.get_loc(None) should raise KeyError, not TypeError + ser = pd.Series([2, 5, 6, 8], index=[2.0, 4.0, 4.0, 5.0]) + with pytest.raises(KeyError): + ser[None] + + with pytest.raises(KeyError): + ser.index.get_loc(None) + + with pytest.raises(KeyError): + ser.index._engine.get_loc(None) From f3e6147f45e8ec4829bec16025b84640b8f46b3f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 18 Nov 2019 16:43:23 -0800 Subject: [PATCH 2/3] blackify --- pandas/tests/groupby/test_groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index d175d7fb1072b..b69847e27d75c 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -1959,5 +1959,5 @@ def test_groupby_duplicate_index(): gb = ser.groupby(level=0) result = gb.mean() - expected = pd.Series([2, 5.5, 8], index=[2., 4., 5.]) + expected = pd.Series([2, 5.5, 8], index=[2.0, 4.0, 5.0]) tm.assert_series_equal(result, expected) From edcf81869c8a080b34e598f47b5729e244197c93 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 21 Nov 2019 11:34:41 -0800 Subject: [PATCH 3/3] whatsnew --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 54640ff576338..76aecf8a27a2a 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -453,7 +453,7 @@ Indexing - Bug in :meth:`Float64Index.astype` where ``np.inf`` was not handled properly when casting to an integer dtype (:issue:`28475`) - :meth:`Index.union` could fail when the left contained duplicates (:issue:`28257`) - :meth:`Index.get_indexer_non_unique` could fail with `TypeError` in some cases, such as when searching for ints in a string index (:issue:`28257`) -- +- Bug in :meth:`Float64Index.get_loc` incorrectly raising ``TypeError`` instead of ``KeyError`` (:issue:`29189`) Missing ^^^^^^^