@@ -742,7 +742,12 @@ def _get_setitem_indexer(self, key):
742742
743743 ax = self .obj ._get_axis (0 )
744744
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+ ):
746751 with suppress (KeyError , InvalidIndexError ):
747752 # TypeError e.g. passed a bool
748753 return ax .get_loc (key )
@@ -1063,6 +1068,14 @@ def _getitem_nested_tuple(self, tup: tuple):
10631068 # we have a nested tuple so have at least 1 multi-index level
10641069 # we should be able to match up the dimensionality here
10651070
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+
10661079 for key in tup :
10671080 check_dict_or_set_indexers (key )
10681081
@@ -1073,7 +1086,10 @@ def _getitem_nested_tuple(self, tup: tuple):
10731086 if self .name != "loc" :
10741087 # This should never be reached, but let's be explicit about it
10751088 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+ ):
10771093 # GH#10521 Series should reduce MultiIndex dimensions instead of
10781094 # DataFrame, IndexingError is not raised when slice(None,None,None)
10791095 # with one row.
@@ -1422,7 +1438,15 @@ def _convert_to_indexer(self, key, axis: AxisInt):
14221438 ):
14231439 raise IndexingError ("Too many indexers" )
14241440
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+ ):
14261450 # Otherwise get_loc will raise InvalidIndexError
14271451
14281452 # if we are a label return me
0 commit comments