Skip to content

Commit

Permalink
REF: define _get_slice_axis in correct classes (#31304)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jan 26, 2020
1 parent 6f93898 commit 8b4f973
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,16 +1662,6 @@ def _convert_to_indexer(self, key, axis: int):
return {"key": key}
raise

def _get_slice_axis(self, slice_obj: slice, axis: int):
# caller is responsible for ensuring non-None axis
obj = self.obj

if not need_slice(slice_obj):
return obj.copy(deep=False)

indexer = self._convert_slice_indexer(slice_obj, axis)
return self._slice(indexer, axis=axis, kind="iloc")


class _LocationIndexer(_NDFrameIndexer):
_takeable: bool = False
Expand Down Expand Up @@ -1706,27 +1696,6 @@ def _getbool_axis(self, key, axis: int):
inds = key.nonzero()[0]
return self.obj.take(inds, axis=axis)

def _get_slice_axis(self, slice_obj: slice, axis: int):
"""
This is pretty simple as we just have to deal with labels.
"""
# caller is responsible for ensuring non-None axis
obj = self.obj
if not need_slice(slice_obj):
return obj.copy(deep=False)

labels = obj._get_axis(axis)
indexer = labels.slice_indexer(
slice_obj.start, slice_obj.stop, slice_obj.step, kind=self.name
)

if isinstance(indexer, slice):
return self._slice(indexer, axis=axis, kind="iloc")
else:
# DatetimeIndex overrides Index.slice_indexer and may
# return a DatetimeIndex instead of a slice object.
return self.obj.take(indexer, axis=axis)


@Appender(IndexingMixin.loc.__doc__)
class _LocIndexer(_LocationIndexer):
Expand Down Expand Up @@ -1881,14 +1850,34 @@ def _getitem_axis(self, key, axis: int):
self._validate_key(key, axis)
return self._get_label(key, axis=axis)

def _get_slice_axis(self, slice_obj: slice, axis: int):
"""
This is pretty simple as we just have to deal with labels.
"""
# caller is responsible for ensuring non-None axis
obj = self.obj
if not need_slice(slice_obj):
return obj.copy(deep=False)

labels = obj._get_axis(axis)
indexer = labels.slice_indexer(
slice_obj.start, slice_obj.stop, slice_obj.step, kind=self.name
)

if isinstance(indexer, slice):
return self._slice(indexer, axis=axis, kind="iloc")
else:
# DatetimeIndex overrides Index.slice_indexer and may
# return a DatetimeIndex instead of a slice object.
return self.obj.take(indexer, axis=axis)


@Appender(IndexingMixin.iloc.__doc__)
class _iLocIndexer(_LocationIndexer):
_valid_types = (
"integer, integer slice (START point is INCLUDED, END "
"point is EXCLUDED), listlike of integers, boolean array"
)
_get_slice_axis = _NDFrameIndexer._get_slice_axis
_takeable = True

def _validate_key(self, key, axis: int):
Expand Down Expand Up @@ -2051,6 +2040,16 @@ def _getitem_axis(self, key, axis: int):

return self._get_loc(key, axis=axis)

def _get_slice_axis(self, slice_obj: slice, axis: int):
# caller is responsible for ensuring non-None axis
obj = self.obj

if not need_slice(slice_obj):
return obj.copy(deep=False)

indexer = self._convert_slice_indexer(slice_obj, axis)
return self._slice(indexer, axis=axis, kind="iloc")

def _convert_to_indexer(self, key, axis: int):
"""
Much simpler as we only have to deal with our valid types.
Expand Down

0 comments on commit 8b4f973

Please sign in to comment.