@@ -889,21 +889,19 @@ def __getitem__(self, key):
889
889
elif key_is_scalar :
890
890
return self ._get_value (key )
891
891
892
- if (
893
- isinstance (key , tuple )
894
- and is_hashable (key )
895
- and isinstance (self .index , MultiIndex )
896
- ):
892
+ if is_hashable (key ):
897
893
# Otherwise index.get_value will raise InvalidIndexError
898
894
try :
895
+ # For labels that don't resolve as scalars like tuples and frozensets
899
896
result = self ._get_value (key )
900
897
901
898
return result
902
899
903
900
except KeyError :
904
- # We still have the corner case where this tuple is a key
905
- # in the first level of our MultiIndex
906
- return self ._get_values_tuple (key )
901
+ if isinstance (key , tuple ) and isinstance (self .index , MultiIndex ):
902
+ # We still have the corner case where a tuple is a key
903
+ # in the first level of our MultiIndex
904
+ return self ._get_values_tuple (key )
907
905
908
906
if is_iterator (key ):
909
907
key = list (key )
@@ -963,7 +961,7 @@ def _get_values_tuple(self, key):
963
961
return result
964
962
965
963
if not isinstance (self .index , MultiIndex ):
966
- raise ValueError ("Can only tuple-index with a MultiIndex" )
964
+ raise ValueError ("key of type tuple not found and not a MultiIndex" )
967
965
968
966
# If key is contained, would have returned by now
969
967
indexer , new_index = self .index .get_loc_level (key )
@@ -1017,9 +1015,11 @@ def __setitem__(self, key, value):
1017
1015
# GH#12862 adding an new key to the Series
1018
1016
self .loc [key ] = value
1019
1017
1020
- except TypeError as e :
1018
+ except TypeError as err :
1021
1019
if isinstance (key , tuple ) and not isinstance (self .index , MultiIndex ):
1022
- raise ValueError ("Can only tuple-index with a MultiIndex" ) from e
1020
+ raise ValueError (
1021
+ "key of type tuple not found and not a MultiIndex"
1022
+ ) from err
1023
1023
1024
1024
if com .is_bool_indexer (key ):
1025
1025
key = check_bool_indexer (self .index , key )
0 commit comments