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