Skip to content

Commit

Permalink
Backport PR #48662 on branch 1.5.x (BUG: Series.getitem not falling b…
Browse files Browse the repository at this point in the history
…ack 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 <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Sep 27, 2022
1 parent 2dfbe0c commit b0bfbee
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
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

0 comments on commit b0bfbee

Please sign in to comment.