Skip to content

Commit 27507a7

Browse files
machenityjreback
authored andcommitted
Fix index locator cast bool key to float, closes pandas-dev#19087 (pandas-dev#22357)
1 parent 45c0b5f commit 27507a7

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ Indexing
656656
- Fixed ``DataFrame[np.nan]`` when columns are non-unique (:issue:`21428`)
657657
- Bug when indexing :class:`DatetimeIndex` with nanosecond resolution dates and timezones (:issue:`11679`)
658658
- Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`)
659+
- ``Float64Index.get_loc`` now raises ``KeyError`` when boolean key passed. (:issue:`19087`)
659660

660661
Missing
661662
^^^^^^^

pandas/core/indexes/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def __contains__(self, other):
403403
@Appender(_index_shared_docs['get_loc'])
404404
def get_loc(self, key, method=None, tolerance=None):
405405
try:
406-
if np.all(np.isnan(key)):
406+
if np.all(np.isnan(key)) or is_bool(key):
407407
nan_idxs = self._nan_idxs
408408
try:
409409
return nan_idxs.item()

pandas/tests/indexes/test_numeric.py

+2
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ def test_get_loc(self):
289289
pytest.raises(KeyError, idx.get_loc, 1.5)
290290
pytest.raises(KeyError, idx.get_loc, 1.5, method='pad',
291291
tolerance=0.1)
292+
pytest.raises(KeyError, idx.get_loc, True)
293+
pytest.raises(KeyError, idx.get_loc, False)
292294

293295
with tm.assert_raises_regex(ValueError, 'must be numeric'):
294296
idx.get_loc(1.4, method='nearest', tolerance='foo')

0 commit comments

Comments
 (0)