Skip to content

REF/TYP: stricter typing for Series._slice #52190

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 1 commit into from
Mar 27, 2023
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
12 changes: 2 additions & 10 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3758,18 +3758,10 @@ def __getitem__(self, key):

elif is_mi and self.columns.is_unique and key in self.columns:
return self._getitem_multilevel(key)

# Do we have a slicer (on rows)?
if isinstance(key, slice):
indexer = self.index._convert_slice_indexer(key, kind="getitem")
if isinstance(indexer, np.ndarray):
# reachable with DatetimeIndex
indexer = lib.maybe_indices_to_slice(
indexer.astype(np.intp, copy=False), len(self)
)
if isinstance(indexer, np.ndarray):
# GH#43223 If we can not convert, use take
return self.take(indexer, axis=0)
return self._slice(indexer, axis=0)
return self._getitem_slice(key)

# Do we have a (boolean) DataFrame?
if isinstance(key, DataFrame):
Expand Down
21 changes: 20 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4141,7 +4141,26 @@ class animal locomotion
def __getitem__(self, item):
raise AbstractMethodError(self)

def _slice(self, slobj: slice, axis: Axis = 0) -> Self:
@final
def _getitem_slice(self, key: slice) -> Self:
"""
__getitem__ for the case where the key is a slice object.
"""
# _convert_slice_indexer to determine if this slice is positional
# or label based, and if the latter, convert to positional
slobj = self.index._convert_slice_indexer(key, kind="getitem")
if isinstance(slobj, np.ndarray):
# reachable with DatetimeIndex
indexer = lib.maybe_indices_to_slice(
slobj.astype(np.intp, copy=False), len(self)
)
if isinstance(indexer, np.ndarray):
# GH#43223 If we can not convert, use take
return self.take(indexer, axis=0)
slobj = indexer
return self._slice(slobj)

def _slice(self, slobj: slice, axis: AxisInt = 0) -> Self:
"""
Construct a slice of this container.

Expand Down
11 changes: 5 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,12 @@ def _ixs(self, i: int, axis: AxisInt = 0) -> Any:
"""
return self._values[i]

def _slice(self, slobj: slice | np.ndarray, axis: Axis = 0) -> Series:
def _slice(self, slobj: slice, axis: AxisInt = 0) -> Series:
# axis kwarg is retained for compat with NDFrame method
# _slice is *always* positional
return self._get_values(slobj)
mgr = self._mgr.get_slice(slobj, axis=axis)
out = self._constructor(mgr, fastpath=True)
return out.__finalize__(self)

def __getitem__(self, key):
check_dict_or_set_indexers(key)
Expand Down Expand Up @@ -983,10 +985,7 @@ def __getitem__(self, key):

if isinstance(key, slice):
# Do slice check before somewhat-costly is_bool_indexer
# _convert_slice_indexer to determine 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)
return self._getitem_slice(key)

if is_iterator(key):
key = list(key)
Expand Down