@@ -742,7 +742,12 @@ def _get_setitem_indexer(self, key):
742
742
743
743
ax = self .obj ._get_axis (0 )
744
744
745
- if isinstance (ax , MultiIndex ) and self .name != "iloc" and is_hashable (key ):
745
+ if (
746
+ isinstance (ax , MultiIndex )
747
+ and self .name != "iloc"
748
+ and is_hashable (key )
749
+ and not isinstance (key , slice )
750
+ ):
746
751
with suppress (KeyError , InvalidIndexError ):
747
752
# TypeError e.g. passed a bool
748
753
return ax .get_loc (key )
@@ -1063,6 +1068,14 @@ def _getitem_nested_tuple(self, tup: tuple):
1063
1068
# we have a nested tuple so have at least 1 multi-index level
1064
1069
# we should be able to match up the dimensionality here
1065
1070
1071
+ def _contains_slice (x : object ) -> bool :
1072
+ # Check if object is a slice or a tuple containing a slice
1073
+ if isinstance (x , tuple ):
1074
+ return any (isinstance (v , slice ) for v in x )
1075
+ elif isinstance (x , slice ):
1076
+ return True
1077
+ return False
1078
+
1066
1079
for key in tup :
1067
1080
check_dict_or_set_indexers (key )
1068
1081
@@ -1073,7 +1086,10 @@ def _getitem_nested_tuple(self, tup: tuple):
1073
1086
if self .name != "loc" :
1074
1087
# This should never be reached, but let's be explicit about it
1075
1088
raise ValueError ("Too many indices" ) # pragma: no cover
1076
- if all (is_hashable (x ) or com .is_null_slice (x ) for x in tup ):
1089
+ if all (
1090
+ (is_hashable (x ) and not _contains_slice (x )) or com .is_null_slice (x )
1091
+ for x in tup
1092
+ ):
1077
1093
# GH#10521 Series should reduce MultiIndex dimensions instead of
1078
1094
# DataFrame, IndexingError is not raised when slice(None,None,None)
1079
1095
# with one row.
@@ -1422,7 +1438,15 @@ def _convert_to_indexer(self, key, axis: AxisInt):
1422
1438
):
1423
1439
raise IndexingError ("Too many indexers" )
1424
1440
1425
- if is_scalar (key ) or (isinstance (labels , MultiIndex ) and is_hashable (key )):
1441
+ # Slices are not valid keys passed in by the user,
1442
+ # even though they are hashable in Python 3.12
1443
+ contains_slice = False
1444
+ if isinstance (key , tuple ):
1445
+ contains_slice = any (isinstance (v , slice ) for v in key )
1446
+
1447
+ if is_scalar (key ) or (
1448
+ isinstance (labels , MultiIndex ) and is_hashable (key ) and not contains_slice
1449
+ ):
1426
1450
# Otherwise get_loc will raise InvalidIndexError
1427
1451
1428
1452
# if we are a label return me
0 commit comments