Skip to content

Commit

Permalink
CLN: refactor _getitem_axis() of _iLocIndexer class
Browse files Browse the repository at this point in the history
 _get_list_axis() is factored out of _getitem_axis() to handle list-like type of input.
  • Loading branch information
nathalier committed Nov 13, 2016
1 parent ff52ac7 commit 41b77ca
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,27 @@ def _get_slice_axis(self, slice_obj, axis=0):
else:
return self.obj.take(slice_obj, axis=axis, convert=False)

def _get_list_axis(self, key_list, axis=0):
"""
Return Series values by list or array of integers
Parameters
----------
key_list : list-like positional indexer
axis : int (can only be zero)
Returns
-------
Series object
"""

# validate list bounds
self._is_valid_list_like(key_list, axis)

# force an actual list
key_list = list(key_list)
return self.obj.take(key_list, axis=axis, convert=False)

def _getitem_axis(self, key, axis=0):

if isinstance(key, slice):
Expand All @@ -1605,27 +1626,20 @@ def _getitem_axis(self, key, axis=0):
self._has_valid_type(key, axis)
return self._getbool_axis(key, axis=axis)

# a single integer or a list of integers
else:

if is_list_like_indexer(key):

# validate list bounds
self._is_valid_list_like(key, axis)

# force an actual list
key = list(key)
return self.obj.take(key, axis=axis, convert=False)
# a list of integers
elif is_list_like_indexer(key):
return self._get_list_axis(key, axis=axis)

else:
key = self._convert_scalar_indexer(key, axis)
# a single integer
else:
key = self._convert_scalar_indexer(key, axis)

if not is_integer(key):
raise TypeError("Cannot index by location index with a "
"non-integer key")
if not is_integer(key):
raise TypeError("Cannot index by location index with a "
"non-integer key")

# validate the location
self._is_valid_integer(key, axis)
# validate the location
self._is_valid_integer(key, axis)

return self._get_loc(key, axis=axis)

Expand Down

0 comments on commit 41b77ca

Please sign in to comment.