Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #48662 on branch 1.5.x (BUG: Series.getitem not falling back to positional for bool index) #48799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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`)
-
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5971,7 +5971,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):
"""
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down