Skip to content

Commit 6f065b6

Browse files
authored
PERF: faster indexing for non-fastpath groupby ops (#34214)
1 parent 35a1657 commit 6f065b6

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

pandas/core/groupby/ops.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,9 @@ def _chop(self, sdata, slice_obj: slice) -> NDFrame:
952952

953953
class SeriesSplitter(DataSplitter):
954954
def _chop(self, sdata: Series, slice_obj: slice) -> Series:
955-
return sdata.iloc[slice_obj]
955+
# fastpath equivalent to `sdata.iloc[slice_obj]`
956+
mgr = sdata._mgr.get_slice(slice_obj)
957+
return type(sdata)(mgr, name=sdata.name, fastpath=True)
956958

957959

958960
class FrameSplitter(DataSplitter):
@@ -962,10 +964,13 @@ def fast_apply(self, f: F, sdata: FrameOrSeries, names):
962964
return libreduction.apply_frame_axis0(sdata, f, names, starts, ends)
963965

964966
def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame:
965-
if self.axis == 0:
966-
return sdata.iloc[slice_obj]
967-
else:
968-
return sdata.iloc[:, slice_obj]
967+
# Fastpath equivalent to:
968+
# if self.axis == 0:
969+
# return sdata.iloc[slice_obj]
970+
# else:
971+
# return sdata.iloc[:, slice_obj]
972+
mgr = sdata._mgr.get_slice(slice_obj, axis=1 - self.axis)
973+
return type(sdata)(mgr)
969974

970975

971976
def get_splitter(

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def make_block_same_class(self, values, placement=None, ndim=None):
251251
placement = self.mgr_locs
252252
if ndim is None:
253253
ndim = self.ndim
254-
return make_block(values, placement=placement, ndim=ndim, klass=type(self))
254+
return type(self)(values, placement=placement, ndim=ndim)
255255

256256
def __repr__(self) -> str:
257257
# don't want to print out all of the items here

0 commit comments

Comments
 (0)