Skip to content

Commit 1847584

Browse files
committed
REF: simplify _validate_key
1 parent aaeed3e commit 1847584

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

pandas/core/indexing.py

+14-29
Original file line numberDiff line numberDiff line change
@@ -1933,9 +1933,18 @@ def _validate_key(self, key, axis):
19331933
if isinstance(key, slice):
19341934
return
19351935
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')
19371941
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")
19391948
else:
19401949
raise ValueError("Can only index by location with "
19411950
"a [{types}]".format(types=self._valid_types))
@@ -1970,33 +1979,13 @@ def _getitem_scalar(self, key):
19701979
values = self.obj._get_value(*key, takeable=True)
19711980
return values
19721981

1973-
def _is_valid_integer(self, key, axis):
1982+
def _validate_integer(self, key, axis):
19741983
# return a boolean if we have a valid integer indexer
19751984

19761985
ax = self.obj._get_axis(axis)
19771986
l = len(ax)
19781987
if key >= l or key < -l:
19791988
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
20001989

20011990
def _getitem_tuple(self, tup):
20021991

@@ -2067,14 +2056,10 @@ def _getitem_axis(self, key, axis=None):
20672056
axis = self.axis or 0
20682057

20692058
if isinstance(key, slice):
2070-
self._validate_key(key, axis)
20712059
return self._get_slice_axis(key, axis=axis)
20722060

20732061
if isinstance(key, list):
2074-
try:
2075-
key = np.asarray(key)
2076-
except TypeError: # pragma: no cover
2077-
pass
2062+
key = np.asarray(key)
20782063

20792064
if com.is_bool_indexer(key):
20802065
self._validate_key(key, axis)
@@ -2093,7 +2078,7 @@ def _getitem_axis(self, key, axis=None):
20932078
"non-integer key")
20942079

20952080
# validate the location
2096-
self._is_valid_integer(key, axis)
2081+
self._validate_integer(key, axis)
20972082

20982083
return self._get_loc(key, axis=axis)
20992084

0 commit comments

Comments
 (0)