diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index da0bd746e3da5..6936baee5a9f8 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -82,6 +82,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ +- Bug in :meth:`Series.__getitem__` not falling back to positional for integer keys and boolean :class:`Index` (:issue:`48653`) - Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`) - Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`) - diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7dc04474cbcd8..e061c91bb7321 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6017,7 +6017,7 @@ def _should_fallback_to_positional(self) -> bool: """ Should an integer key be treated as positional? """ - return not self.holds_integer() and not self.is_boolean() + return not self.holds_integer() def _get_values_for_loc(self, series: Series, loc, key): """ diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 111c1652240e2..cc67dd9caeea9 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -201,6 +201,12 @@ def test_getitem_str_with_timedeltaindex(self): with pytest.raises(KeyError, match=msg): ser["50 days"] + def test_getitem_bool_index_positional(self): + # GH#48653 + ser = Series({True: 1, False: 0}) + result = ser[0] + assert result == 1 + class TestSeriesGetitemSlices: def test_getitem_partial_str_slice_with_datetimeindex(self): diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 0bab391dab27d..e3df9671e6c64 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -367,6 +367,13 @@ def test_loc_setitem_nested_data_enlargement(): tm.assert_series_equal(ser, expected) +def test_getitem_bool_int_key(): + # GH#48653 + ser = Series({True: 1, False: 0}) + with pytest.raises(KeyError, match="0"): + ser.loc[0] + + class TestDeprecatedIndexers: @pytest.mark.parametrize("key", [{1}, {1: 1}]) def test_getitem_dict_and_set_deprecated(self, key):