@@ -1932,9 +1932,18 @@ def _validate_key(self, key, axis):
1932
1932
if isinstance (key , slice ):
1933
1933
return
1934
1934
elif is_integer (key ):
1935
- assert (self ._is_valid_integer (key , axis ))
1935
+ self ._validate_integer (key , axis )
1936
+ elif isinstance (key , tuple ):
1937
+ # a tuple should already have been caught by this point
1938
+ # so don't treat a tuple as a valid indexer
1939
+ raise IndexingError ('Too many indexers' )
1936
1940
elif is_list_like_indexer (key ):
1937
- assert (self ._is_valid_list_like (key , axis ))
1941
+ # check that the key does not exceed the maximum size of the index
1942
+ arr = np .array (key )
1943
+ l = len (self .obj ._get_axis (axis ))
1944
+
1945
+ if len (arr ) and (arr .max () >= l or arr .min () < - l ):
1946
+ raise IndexError ("positional indexers are out-of-bounds" )
1938
1947
else :
1939
1948
raise ValueError ("Can only index by location with "
1940
1949
"a [{types}]" .format (types = self ._valid_types ))
@@ -1969,33 +1978,13 @@ def _getitem_scalar(self, key):
1969
1978
values = self .obj ._get_value (* key , takeable = True )
1970
1979
return values
1971
1980
1972
- def _is_valid_integer (self , key , axis ):
1981
+ def _validate_integer (self , key , axis ):
1973
1982
# return a boolean if we have a valid integer indexer
1974
1983
1975
1984
ax = self .obj ._get_axis (axis )
1976
1985
l = len (ax )
1977
1986
if key >= l or key < - l :
1978
1987
raise IndexError ("single positional indexer is out-of-bounds" )
1979
- return True
1980
-
1981
- def _is_valid_list_like (self , key , axis ):
1982
- # return a boolean if we are a valid list-like (e.g. that we don't
1983
- # have out-of-bounds values)
1984
-
1985
- # a tuple should already have been caught by this point
1986
- # so don't treat a tuple as a valid indexer
1987
- if isinstance (key , tuple ):
1988
- raise IndexingError ('Too many indexers' )
1989
-
1990
- # coerce the key to not exceed the maximum size of the index
1991
- arr = np .array (key )
1992
- ax = self .obj ._get_axis (axis )
1993
- l = len (ax )
1994
- if (hasattr (arr , '__len__' ) and len (arr ) and
1995
- (arr .max () >= l or arr .min () < - l )):
1996
- raise IndexError ("positional indexers are out-of-bounds" )
1997
-
1998
- return True
1999
1988
2000
1989
def _getitem_tuple (self , tup ):
2001
1990
@@ -2066,14 +2055,10 @@ def _getitem_axis(self, key, axis=None):
2066
2055
axis = self .axis or 0
2067
2056
2068
2057
if isinstance (key , slice ):
2069
- self ._validate_key (key , axis )
2070
2058
return self ._get_slice_axis (key , axis = axis )
2071
2059
2072
2060
if isinstance (key , list ):
2073
- try :
2074
- key = np .asarray (key )
2075
- except TypeError : # pragma: no cover
2076
- pass
2061
+ key = np .asarray (key )
2077
2062
2078
2063
if com .is_bool_indexer (key ):
2079
2064
self ._validate_key (key , axis )
@@ -2092,7 +2077,7 @@ def _getitem_axis(self, key, axis=None):
2092
2077
"non-integer key" )
2093
2078
2094
2079
# validate the location
2095
- self ._is_valid_integer (key , axis )
2080
+ self ._validate_integer (key , axis )
2096
2081
2097
2082
return self ._get_loc (key , axis = axis )
2098
2083
0 commit comments