Skip to content

Commit 34ee4fa

Browse files
authored
REF/TYP: stricter typing for Series._slice (#52190)
1 parent 606e0fe commit 34ee4fa

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

pandas/core/frame.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -3758,18 +3758,10 @@ def __getitem__(self, key):
37583758

37593759
elif is_mi and self.columns.is_unique and key in self.columns:
37603760
return self._getitem_multilevel(key)
3761+
37613762
# Do we have a slicer (on rows)?
37623763
if isinstance(key, slice):
3763-
indexer = self.index._convert_slice_indexer(key, kind="getitem")
3764-
if isinstance(indexer, np.ndarray):
3765-
# reachable with DatetimeIndex
3766-
indexer = lib.maybe_indices_to_slice(
3767-
indexer.astype(np.intp, copy=False), len(self)
3768-
)
3769-
if isinstance(indexer, np.ndarray):
3770-
# GH#43223 If we can not convert, use take
3771-
return self.take(indexer, axis=0)
3772-
return self._slice(indexer, axis=0)
3764+
return self._getitem_slice(key)
37733765

37743766
# Do we have a (boolean) DataFrame?
37753767
if isinstance(key, DataFrame):

pandas/core/generic.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -4150,7 +4150,26 @@ class animal locomotion
41504150
def __getitem__(self, item):
41514151
raise AbstractMethodError(self)
41524152

4153-
def _slice(self, slobj: slice, axis: Axis = 0) -> Self:
4153+
@final
4154+
def _getitem_slice(self, key: slice) -> Self:
4155+
"""
4156+
__getitem__ for the case where the key is a slice object.
4157+
"""
4158+
# _convert_slice_indexer to determine if this slice is positional
4159+
# or label based, and if the latter, convert to positional
4160+
slobj = self.index._convert_slice_indexer(key, kind="getitem")
4161+
if isinstance(slobj, np.ndarray):
4162+
# reachable with DatetimeIndex
4163+
indexer = lib.maybe_indices_to_slice(
4164+
slobj.astype(np.intp, copy=False), len(self)
4165+
)
4166+
if isinstance(indexer, np.ndarray):
4167+
# GH#43223 If we can not convert, use take
4168+
return self.take(indexer, axis=0)
4169+
slobj = indexer
4170+
return self._slice(slobj)
4171+
4172+
def _slice(self, slobj: slice, axis: AxisInt = 0) -> Self:
41544173
"""
41554174
Construct a slice of this container.
41564175

pandas/core/series.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,12 @@ def _ixs(self, i: int, axis: AxisInt = 0) -> Any:
943943
"""
944944
return self._values[i]
945945

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

951953
def __getitem__(self, key):
952954
check_dict_or_set_indexers(key)
@@ -983,10 +985,7 @@ def __getitem__(self, key):
983985

984986
if isinstance(key, slice):
985987
# Do slice check before somewhat-costly is_bool_indexer
986-
# _convert_slice_indexer to determine if this slice is positional
987-
# or label based, and if the latter, convert to positional
988-
slobj = self.index._convert_slice_indexer(key, kind="getitem")
989-
return self._slice(slobj)
988+
return self._getitem_slice(key)
990989

991990
if is_iterator(key):
992991
key = list(key)

0 commit comments

Comments
 (0)