Skip to content

Commit 62793a4

Browse files
committed
add allow_slice=False
1 parent fd30506 commit 62793a4

File tree

3 files changed

+5
-11
lines changed

3 files changed

+5
-11
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4050,7 +4050,7 @@ def __getitem__(self, key):
40504050
key = lib.item_from_zerodim(key)
40514051
key = com.apply_if_callable(key, self)
40524052

4053-
if is_hashable(key) and not is_iterator(key) and not isinstance(key, slice):
4053+
if is_hashable(key, allow_slice=False) and not is_iterator(key):
40544054
# is_iterator to exclude generator e.g. test_getitem_listlike
40554055
# As of Python 3.12, slice is hashable which breaks MultiIndex (GH#57500)
40564056

pandas/core/indexing.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,7 @@ def _get_setitem_indexer(self, key):
793793
if (
794794
isinstance(ax, MultiIndex)
795795
and self.name != "iloc"
796-
and is_hashable(key)
797-
and not isinstance(key, slice)
796+
and is_hashable(key, allow_slice=False)
798797
):
799798
with suppress(KeyError, InvalidIndexError):
800799
# TypeError e.g. passed a bool
@@ -1147,8 +1146,7 @@ def _contains_slice(x: object) -> bool:
11471146
# This should never be reached, but let's be explicit about it
11481147
raise ValueError("Too many indices") # pragma: no cover
11491148
if all(
1150-
(is_hashable(x) and not _contains_slice(x)) or com.is_null_slice(x)
1151-
for x in tup
1149+
is_hashable(x, allow_slice=False) or com.is_null_slice(x) for x in tup
11521150
):
11531151
# GH#10521 Series should reduce MultiIndex dimensions instead of
11541152
# DataFrame, IndexingError is not raised when slice(None,None,None)
@@ -1511,12 +1509,8 @@ def _convert_to_indexer(self, key, axis: AxisInt):
15111509

15121510
# Slices are not valid keys passed in by the user,
15131511
# even though they are hashable in Python 3.12
1514-
contains_slice = False
1515-
if isinstance(key, tuple):
1516-
contains_slice = any(isinstance(v, slice) for v in key)
1517-
15181512
if is_scalar(key) or (
1519-
isinstance(labels, MultiIndex) and is_hashable(key) and not contains_slice
1513+
isinstance(labels, MultiIndex) and is_hashable(key, allow_slice=False)
15201514
):
15211515
# Otherwise get_loc will raise InvalidIndexError
15221516

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def __getitem__(self, key):
953953
if is_iterator(key):
954954
key = list(key)
955955

956-
if is_hashable(key) and not isinstance(key, slice):
956+
if is_hashable(key, allow_slice=False):
957957
# Otherwise index.get_value will raise InvalidIndexError
958958
try:
959959
# For labels that don't resolve as scalars like tuples and frozensets

0 commit comments

Comments
 (0)