Skip to content

REF: share _wrap_transform_fast_result #43538

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
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
23 changes: 0 additions & 23 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,6 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
def _can_use_transform_fast(self, result) -> bool:
return True

def _wrap_transform_fast_result(self, result: Series) -> Series:
"""
fast version of transform, only applicable to
builtin/cythonizable functions
"""
ids, _, _ = self.grouper.group_info
result = result.reindex(self.grouper.result_index, copy=False)
out = algorithms.take_nd(result._values, ids)
return self.obj._constructor(out, index=self.obj.index, name=self.obj.name)

def filter(self, func, dropna: bool = True, *args, **kwargs):
"""
Return a copy of a Series excluding elements from groups that
Expand Down Expand Up @@ -1274,19 +1264,6 @@ def _can_use_transform_fast(self, result) -> bool:
self._obj_with_exclusions.columns
)

def _wrap_transform_fast_result(self, result: DataFrame) -> DataFrame:
"""
Fast transform path for aggregations
"""
obj = self._obj_with_exclusions

# for each col, reshape to size of original frame by take operation
ids, _, _ = self.grouper.group_info
result = result.reindex(self.grouper.result_index, copy=False)
output = result.take(ids, axis=0)
output.index = obj.index
return output

def _define_paths(self, func, *args, **kwargs):
if isinstance(func, str):
fast_path = lambda group: getattr(group, func)(*args, **kwargs)
Expand Down
20 changes: 20 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,26 @@ def _transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):
# only reached for DataFrameGroupBy
return self._transform_general(func, *args, **kwargs)

@final
def _wrap_transform_fast_result(self, result: FrameOrSeries) -> FrameOrSeries:
"""
Fast transform path for aggregations.
"""
obj = self._obj_with_exclusions

# for each col, reshape to size of original frame by take operation
ids, _, _ = self.grouper.group_info
result = result.reindex(self.grouper.result_index, copy=False)

if self.obj.ndim == 1:
# i.e. SeriesGroupBy
out = algorithms.take_nd(result._values, ids)
output = obj._constructor(out, index=obj.index, name=obj.name)
else:
output = result.take(ids, axis=0)
output.index = obj.index
return output

# -----------------------------------------------------------------
# Utilities

Expand Down