Skip to content

Commit b0bfbee

Browse files
Backport PR #48662 on branch 1.5.x (BUG: Series.getitem not falling back to positional for bool index) (#48799)
Backport PR #48662: BUG: Series.getitem not falling back to positional for bool index Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 2dfbe0c commit b0bfbee

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Fixed regressions
8383

8484
Bug fixes
8585
~~~~~~~~~
86+
- Bug in :meth:`Series.__getitem__` not falling back to positional for integer keys and boolean :class:`Index` (:issue:`48653`)
8687
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
8788
- Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`)
8889
-

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5971,7 +5971,7 @@ def _should_fallback_to_positional(self) -> bool:
59715971
"""
59725972
Should an integer key be treated as positional?
59735973
"""
5974-
return not self.holds_integer() and not self.is_boolean()
5974+
return not self.holds_integer()
59755975

59765976
def _get_values_for_loc(self, series: Series, loc, key):
59775977
"""

pandas/tests/series/indexing/test_getitem.py

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ def test_getitem_str_with_timedeltaindex(self):
201201
with pytest.raises(KeyError, match=msg):
202202
ser["50 days"]
203203

204+
def test_getitem_bool_index_positional(self):
205+
# GH#48653
206+
ser = Series({True: 1, False: 0})
207+
result = ser[0]
208+
assert result == 1
209+
204210

205211
class TestSeriesGetitemSlices:
206212
def test_getitem_partial_str_slice_with_datetimeindex(self):

pandas/tests/series/indexing/test_indexing.py

+7
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ def test_loc_setitem_nested_data_enlargement():
367367
tm.assert_series_equal(ser, expected)
368368

369369

370+
def test_getitem_bool_int_key():
371+
# GH#48653
372+
ser = Series({True: 1, False: 0})
373+
with pytest.raises(KeyError, match="0"):
374+
ser.loc[0]
375+
376+
370377
class TestDeprecatedIndexers:
371378
@pytest.mark.parametrize("key", [{1}, {1: 1}])
372379
def test_getitem_dict_and_set_deprecated(self, key):

0 commit comments

Comments
 (0)