Skip to content

Commit

Permalink
Reorder slice and hashable in __getitem__
Browse files Browse the repository at this point in the history
  • Loading branch information
tehunter committed Mar 28, 2024
1 parent 192db0d commit 236d3f3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3849,6 +3849,11 @@ def __getitem__(self, key):
key = lib.item_from_zerodim(key)
key = com.apply_if_callable(key, self)

# Do we have a slicer (on rows)?
# As of Python 3.12, slice is hashable so check it first
if isinstance(key, slice):
return self._getitem_slice(key)

if is_hashable(key) and not is_iterator(key):
# is_iterator to exclude generator e.g. test_getitem_listlike
# shortcut if the key is in columns
Expand All @@ -3865,10 +3870,6 @@ def __getitem__(self, key):
elif is_mi and self.columns.is_unique and key in self.columns:
return self._getitem_multilevel(key)

# Do we have a slicer (on rows)?
if isinstance(key, slice):
return self._getitem_slice(key)

# Do we have a (boolean) DataFrame?
if isinstance(key, DataFrame):
return self.where(key)
Expand Down

0 comments on commit 236d3f3

Please sign in to comment.