Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF: define _get_slice_axis in correct classes #31304

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this condition should likely be handled in _slice itself

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, _slice is a one-liner that just calls self.obj._slice, so i plan to get rid of it. for now this is just putting the methods in more reasonable place unchanged

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk

# 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