Skip to content

REF: make Series/DataFrame _slice always positional #31854

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

Merged
merged 2 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3498,13 +3498,11 @@ def _iget_item_cache(self, item):
def _box_item_values(self, key, values):
raise AbstractMethodError(self)

def _slice(
self: FrameOrSeries, slobj: slice, axis=0, kind: str = "getitem"
) -> FrameOrSeries:
def _slice(self: FrameOrSeries, slobj: slice, axis=0) -> FrameOrSeries:
"""
Construct a slice of this container.

kind parameter is maintained for compatibility with Series slicing.
Slicing with this method is *always* positional.
"""
axis = self._get_block_manager_axis(axis)
result = self._constructor(self._data.get_slice(slobj, axis=axis))
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int):
)

if isinstance(indexer, slice):
return self.obj._slice(indexer, axis=axis, kind="iloc")
return self.obj._slice(indexer, axis=axis)
else:
# DatetimeIndex overrides Index.slice_indexer and may
# return a DatetimeIndex instead of a slice object.
Expand Down Expand Up @@ -1553,7 +1553,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int):

labels = obj._get_axis(axis)
labels._validate_positional_slice(slice_obj)
return self.obj._slice(slice_obj, axis=axis, kind="iloc")
return self.obj._slice(slice_obj, axis=axis)

def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
"""
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,12 +840,9 @@ def _ixs(self, i: int, axis: int = 0):
"""
return self._values[i]

def _slice(self, slobj: slice, axis: int = 0, kind: str = "getitem") -> "Series":
assert kind in ["getitem", "iloc"]
if kind == "getitem":
# If called from getitem, we need to determine whether
# this slice is positional or label-based.
slobj = self.index._convert_slice_indexer(slobj, kind="getitem")
def _slice(self, slobj: slice, axis: int = 0) -> "Series":
# axis kwarg is retained for compat with NDFrame method
# _slice is *always* positional
return self._get_values(slobj)

def __getitem__(self, key):
Expand Down Expand Up @@ -889,7 +886,10 @@ def __getitem__(self, key):
def _get_with(self, key):
# other: fancy integer or otherwise
if isinstance(key, slice):
return self._slice(key, kind="getitem")
# _convert_slice_indexer to determing if this slice is positional
# or label based, and if the latter, convert to positional
slobj = self.index._convert_slice_indexer(key, kind="getitem")
return self._slice(slobj)
elif isinstance(key, ABCDataFrame):
raise TypeError(
"Indexing a Series with DataFrame is not "
Expand Down