72
72
sanitize_array ,
73
73
)
74
74
from pandas .core .generic import NDFrame
75
- from pandas .core .indexers import maybe_convert_indices
75
+ from pandas .core .indexers import maybe_convert_indices , unpack_1tuple
76
76
from pandas .core .indexes .accessors import CombinedDatetimelikeProperties
77
77
from pandas .core .indexes .api import (
78
78
Float64Index ,
@@ -851,6 +851,8 @@ def __getitem__(self, key):
851
851
key_is_scalar = is_scalar (key )
852
852
if key_is_scalar :
853
853
key = self .index ._convert_scalar_indexer (key , kind = "getitem" )
854
+ elif isinstance (key , (list , tuple )):
855
+ key = unpack_1tuple (key )
854
856
855
857
if key_is_scalar or isinstance (self .index , MultiIndex ):
856
858
# Otherwise index.get_value will raise InvalidIndexError
@@ -893,16 +895,7 @@ def _get_with(self, key):
893
895
"supported, use the appropriate DataFrame column"
894
896
)
895
897
elif isinstance (key , tuple ):
896
- try :
897
- return self ._get_values_tuple (key )
898
- except ValueError :
899
- # if we don't have a MultiIndex, we may still be able to handle
900
- # a 1-tuple. see test_1tuple_without_multiindex
901
- if len (key ) == 1 :
902
- key = key [0 ]
903
- if isinstance (key , slice ):
904
- return self ._get_values (key )
905
- raise
898
+ return self ._get_values_tuple (key )
906
899
907
900
if not isinstance (key , (list , np .ndarray , ExtensionArray , Series , Index )):
908
901
key = list (key )
@@ -924,26 +917,8 @@ def _get_with(self, key):
924
917
else :
925
918
return self .iloc [key ]
926
919
927
- if isinstance (key , (list , tuple )):
928
- # TODO: de-dup with tuple case handled above?
920
+ if isinstance (key , list ):
929
921
# handle the dup indexing case GH#4246
930
- if len (key ) == 1 and isinstance (key [0 ], slice ):
931
- # [slice(0, 5, None)] will break if you convert to ndarray,
932
- # e.g. as requested by np.median
933
- # FIXME: hack
934
- if isinstance (key , list ):
935
- # GH#31299
936
- warnings .warn (
937
- "Indexing with a single-item list containing a "
938
- "slice is deprecated and will raise in a future "
939
- "version. Pass a tuple instead." ,
940
- FutureWarning ,
941
- stacklevel = 3 ,
942
- )
943
- # TODO: use a message more like numpy's?
944
- key = tuple (key )
945
- return self ._get_values (key )
946
-
947
922
return self .loc [key ]
948
923
949
924
return self .reindex (key )
0 commit comments