Skip to content

Commit 0caf0a2

Browse files
committed
REF: simplify _validate_key
1 parent e2d8785 commit 0caf0a2

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
@@ -1932,9 +1932,18 @@ def _validate_key(self, key, axis):
19321932
if isinstance(key, slice):
19331933
return
19341934
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')
19361940
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")
19381947
else:
19391948
raise ValueError("Can only index by location with "
19401949
"a [{types}]".format(types=self._valid_types))
@@ -1969,33 +1978,13 @@ def _getitem_scalar(self, key):
19691978
values = self.obj._get_value(*key, takeable=True)
19701979
return values
19711980

1972-
def _is_valid_integer(self, key, axis):
1981+
def _validate_integer(self, key, axis):
19731982
# return a boolean if we have a valid integer indexer
19741983

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

20001989
def _getitem_tuple(self, tup):
20011990

@@ -2066,14 +2055,10 @@ def _getitem_axis(self, key, axis=None):
20662055
axis = self.axis or 0
20672056

20682057
if isinstance(key, slice):
2069-
self._validate_key(key, axis)
20702058
return self._get_slice_axis(key, axis=axis)
20712059

20722060
if isinstance(key, list):
2073-
try:
2074-
key = np.asarray(key)
2075-
except TypeError: # pragma: no cover
2076-
pass
2061+
key = np.asarray(key)
20772062

20782063
if com.is_bool_indexer(key):
20792064
self._validate_key(key, axis)
@@ -2092,7 +2077,7 @@ def _getitem_axis(self, key, axis=None):
20922077
"non-integer key")
20932078

20942079
# validate the location
2095-
self._is_valid_integer(key, axis)
2080+
self._validate_integer(key, axis)
20962081

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

0 commit comments

Comments
 (0)