Skip to content

Commit 6c0f952

Browse files
authored
PERF: non-cython groupby.apply (#40236)
1 parent 29bbd6a commit 6c0f952

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

pandas/core/generic.py

+16
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ def _init_mgr(
277277
mgr = mgr.astype(dtype=dtype)
278278
return mgr
279279

280+
@classmethod
281+
def _from_mgr(cls, mgr: Manager):
282+
"""
283+
Fastpath to create a new DataFrame/Series from just a BlockManager/ArrayManager.
284+
285+
Notes
286+
-----
287+
Skips setting `_flags` attribute; caller is responsible for doing so.
288+
"""
289+
obj = cls.__new__(cls)
290+
object.__setattr__(obj, "_is_copy", None)
291+
object.__setattr__(obj, "_mgr", mgr)
292+
object.__setattr__(obj, "_item_cache", {})
293+
object.__setattr__(obj, "_attrs", {})
294+
return obj
295+
280296
# ----------------------------------------------------------------------
281297
# attrs and flags
282298

pandas/core/groupby/ops.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,13 @@ def _chop(self, sdata: Series, slice_obj: slice) -> Series:
10131013
# fastpath equivalent to `sdata.iloc[slice_obj]`
10141014
mgr = sdata._mgr.get_slice(slice_obj)
10151015
# __finalize__ not called here, must be applied by caller if applicable
1016-
return sdata._constructor(mgr, name=sdata.name, fastpath=True)
1016+
1017+
# fastpath equivalent to:
1018+
# `return sdata._constructor(mgr, name=sdata.name, fastpath=True)`
1019+
obj = type(sdata)._from_mgr(mgr)
1020+
object.__setattr__(obj, "_flags", sdata._flags)
1021+
object.__setattr__(obj, "_name", sdata._name)
1022+
return obj
10171023

10181024

10191025
class FrameSplitter(DataSplitter):
@@ -1030,7 +1036,11 @@ def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame:
10301036
# return sdata.iloc[:, slice_obj]
10311037
mgr = sdata._mgr.get_slice(slice_obj, axis=1 - self.axis)
10321038
# __finalize__ not called here, must be applied by caller if applicable
1033-
return sdata._constructor(mgr)
1039+
1040+
# fastpath equivalent to `return sdata._constructor(mgr)`
1041+
obj = type(sdata)._from_mgr(mgr)
1042+
object.__setattr__(obj, "_flags", sdata._flags)
1043+
return obj
10341044

10351045

10361046
def get_splitter(

0 commit comments

Comments
 (0)