@@ -881,21 +881,19 @@ def __getitem__(self, key):
881
881
elif key_is_scalar :
882
882
return self ._get_value (key )
883
883
884
- if (
885
- isinstance (key , tuple )
886
- and is_hashable (key )
887
- and isinstance (self .index , MultiIndex )
888
- ):
884
+ if is_hashable (key ):
889
885
# Otherwise index.get_value will raise InvalidIndexError
890
886
try :
887
+ # For labels that don't resolve as scalars like tuples and frozensets
891
888
result = self ._get_value (key )
892
889
893
890
return result
894
891
895
892
except KeyError :
896
- # We still have the corner case where this tuple is a key
897
- # in the first level of our MultiIndex
898
- return self ._get_values_tuple (key )
893
+ if isinstance (key , tuple ) and isinstance (self .index , MultiIndex ):
894
+ # We still have the corner case where a tuple is a key
895
+ # in the first level of our MultiIndex
896
+ return self ._get_values_tuple (key )
899
897
900
898
if is_iterator (key ):
901
899
key = list (key )
@@ -955,7 +953,7 @@ def _get_values_tuple(self, key):
955
953
return result
956
954
957
955
if not isinstance (self .index , MultiIndex ):
958
- raise ValueError ("Can only tuple-index with a MultiIndex" )
956
+ raise ValueError ("key of type tuple not found and not a MultiIndex" )
959
957
960
958
# If key is contained, would have returned by now
961
959
indexer , new_index = self .index .get_loc_level (key )
@@ -1009,9 +1007,11 @@ def __setitem__(self, key, value):
1009
1007
# GH#12862 adding an new key to the Series
1010
1008
self .loc [key ] = value
1011
1009
1012
- except TypeError as e :
1010
+ except TypeError as err :
1013
1011
if isinstance (key , tuple ) and not isinstance (self .index , MultiIndex ):
1014
- raise ValueError ("Can only tuple-index with a MultiIndex" ) from e
1012
+ raise ValueError (
1013
+ "key of type tuple not found and not a MultiIndex"
1014
+ ) from err
1015
1015
1016
1016
if com .is_bool_indexer (key ):
1017
1017
key = check_bool_indexer (self .index , key )
0 commit comments