From dff350a6beceb4c3fb79045d0381f6ed845c92f6 Mon Sep 17 00:00:00 2001 From: machenity Date: Wed, 15 Aug 2018 14:03:00 +0900 Subject: [PATCH] Fix index locator cast bool key to float, closes #19087 --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/indexes/numeric.py | 2 +- pandas/tests/indexes/test_numeric.py | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index b37261ea79f21..39eae9643b6b4 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -655,6 +655,7 @@ Indexing - Fixed ``DataFrame[np.nan]`` when columns are non-unique (:issue:`21428`) - Bug when indexing :class:`DatetimeIndex` with nanosecond resolution dates and timezones (:issue:`11679`) - Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`) +- ``Float64Index.get_loc`` now raises ``KeyError`` when boolean key passed. (:issue:`19087`) Missing ^^^^^^^ diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index ea392d0b93377..e0627432cbc2e 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -403,7 +403,7 @@ def __contains__(self, other): @Appender(_index_shared_docs['get_loc']) def get_loc(self, key, method=None, tolerance=None): try: - if np.all(np.isnan(key)): + if np.all(np.isnan(key)) or is_bool(key): nan_idxs = self._nan_idxs try: return nan_idxs.item() diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index b6d61187a3fc3..c8aa7f8fd50fd 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -289,6 +289,8 @@ def test_get_loc(self): pytest.raises(KeyError, idx.get_loc, 1.5) pytest.raises(KeyError, idx.get_loc, 1.5, method='pad', tolerance=0.1) + pytest.raises(KeyError, idx.get_loc, True) + pytest.raises(KeyError, idx.get_loc, False) with tm.assert_raises_regex(ValueError, 'must be numeric'): idx.get_loc(1.4, method='nearest', tolerance='foo')