8000 BUG/PERF: groupby.transform with unobserved categories by undermyumbrella1 · Pull Request #58084 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG/PERF: groupby.transform with unobserved categories #58084

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
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Temporarily change observed=True, for groupby.transform
  • Loading branch information
Kei committed Apr 17, 2024
commit a52e7fed90b025e7826bc44d4f22a0262edceed0
3 changes: 3 additions & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2044,8 +2044,11 @@ def _gotitem(self, key, ndim: int, subset=None):
elif ndim == 1:
if subset is None:
subset = self.obj[key]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this line addition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still appears in the diff of this PR.

orig_obj = self.orig_obj if not self.observed else None
return SeriesGroupBy(
subset,
orig_obj,
self.keys,
level=self.level,
grouper=self._grouper,
Expand Down
80 changes: 64 additions & 16 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ class GroupBy(BaseGroupBy[NDFrameT]):
def __init__(
self,
obj: NDFrameT,
orig_obj: NDFrameT | None = None,
keys: _KeysArgType | None = None,
level: IndexLabel | None = None,
grouper: ops.BaseGrouper | None = None,
Expand All @@ -1117,6 +1118,7 @@ def __init__(
self.sort = sort
self.group_keys = group_keys
self.dropna = dropna
self.orig_obj = obj if orig_obj is None else orig_obj

if grouper is None:
grouper, exclusions, obj = get_grouper(
Expand Down Expand Up @@ -1879,24 +1881,70 @@ def _transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):

else:
# i.e. func in base.reduction_kernels
if self.observed:
return self._reduction_kernel_transform(
func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs
)

# GH#30918 Use _transform_fast only when we know func is an aggregation
# If func is a reduction, we need to broadcast the
# result to the whole group. Compute func result
# and deal with possible broadcasting below.
with com.temp_setattr(self, "as_index", True):
# GH#49834 - result needs groups in the index for
# _wrap_transform_fast_result
if func in ["idxmin", "idxmax"]:
func = cast(Literal["idxmin", "idxmax"], func)
result = self._idxmax_idxmin(func, True, *args, **kwargs)
else:
if engine is not None:
kwargs["engine"] = engine
kwargs["engine_kwargs"] = engine_kwargs
result = getattr(self, func)(*args, **kwargs)
grouper, exclusions, obj = get_grouper(
self.orig_obj,
self.keys,
level=self.level,
sort=self.sort,
observed=True,
dropna=self.dropna,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll want to cache this on the groupby instance - we do not want to have to recompute it if the groupby is reused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved, the group by init now accepts observed_grouper, observed_exclusions params

exclusions = frozenset(exclusions) if exclusions else frozenset()
obj_has_not_changed = self.orig_obj.equals(self.obj)

with (
com.temp_setattr(self, "observed", True),
com.temp_setattr(self, "_grouper", grouper),
com.temp_setattr(self, "exclusions", exclusions),
com.temp_setattr(self, "obj", obj, condition=obj_has_not_changed),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we unconditionally set obj here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved, removed setting obj to obj

):
return self._reduction_kernel_transform(
func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs
)

# with com.temp_setattr(self, "as_index", True):
# # GH#49834 - result needs groups in the index for
# # _wrap_transform_fast_result
# if func in ["idxmin", "idxmax"]:
# func = cast(Literal["idxmin", "idxmax"], func)
# result = self._idxmax_idxmin(func, True, *args, **kwargs)
# else:
# if engine is not None:
# kwargs["engine"] = engine
# kwargs["engine_kwargs"] = engine_kwargs
# result = getattr(self, func)(*args, **kwargs)

# print("result with observed = False\n", result.to_string())
# r = self._wrap_transform_fast_result(result)
# print("reindexed result", r.to_string())
# return r

@final
def _reduction_kernel_transform(
self, func, *args, engine=None, engine_kwargs=None, **kwargs
):
# GH#30918 Use _transform_fast only when we know func is an aggregation
# If func is a reduction, we need to broadcast the
# result to the whole group. Compute func result
# and deal with possible broadcasting below.
with com.temp_setattr(self, "as_index", True):
# GH#49834 - result needs groups in the index for
# _wrap_transform_fast_result
if func in ["idxmin", "idxmax"]:
func = cast(Literal["idxmin", "idxmax"], func)
result = self._idxmax_idxmin(func, True, *args, **kwargs)
else:
if engine is not None:
kwargs["engine"] = engine
kwargs["engine_kwargs"] = engine_kwargs
result = getattr(self, func)(*args, **kwargs)

return self._wrap_transform_fast_result(result)
return self._wrap_transform_fast_result(result)

@final
def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:
Expand Down
0