-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
ENH: pd.NamedAgg forwards *args and **kwargs to aggfunc #62729
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
base: main
Are you sure you want to change the base?
Changes from all commits
4e8ccf6
b3b4924
099e078
d9be409
a52f97c
abfbcbb
d5eda51
2a0b78b
1b0b67a
2aa1b70
7efd59d
fccb672
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| TYPE_CHECKING, | ||
| Any, | ||
| Literal, | ||
| NamedTuple, | ||
| Self, | ||
| TypeAlias, | ||
| TypeVar, | ||
| cast, | ||
|
|
@@ -113,11 +113,11 @@ | |
|
|
||
|
|
||
| @set_module("pandas") | ||
| class NamedAgg(NamedTuple): | ||
| class NamedAgg(tuple): | ||
| """ | ||
| Helper for column specific aggregation with control over output column names. | ||
|
|
||
| Subclass of typing.NamedTuple. | ||
| Subclass of tuple. | ||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
@@ -126,26 +126,71 @@ class NamedAgg(NamedTuple): | |
| aggfunc : function or str | ||
| Function to apply to the provided column. If string, the name of a built-in | ||
| pandas function. | ||
| *args, **kwargs : | ||
| Optional positional and keyword arguments passed to ``aggfunc``. | ||
|
|
||
| See Also | ||
| -------- | ||
| DataFrame.groupby : Group DataFrame using a mapper or by a Series of columns. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> df = pd.DataFrame({"key": [1, 1, 2], "a": [-1, 0, 1], 1: [10, 11, 12]}) | ||
| >>> df = pd.DataFrame({"key": [1, 1, 2], "a": [-1, 0, 1], "b": [10, 11, 12]}) | ||
| >>> agg_a = pd.NamedAgg(column="a", aggfunc="min") | ||
| >>> agg_1 = pd.NamedAgg(column=1, aggfunc=lambda x: np.mean(x)) | ||
| >>> df.groupby("key").agg(result_a=agg_a, result_1=agg_1) | ||
| result_a result_1 | ||
| >>> agg_b = pd.NamedAgg(column="b", aggfunc=lambda x: x.mean()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the point here is to demonstrate that you can used a named tuple on columns that are not strings. |
||
| >>> df.groupby("key").agg(result_a=agg_a, result_b=agg_b) | ||
| result_a result_b | ||
| key | ||
| 1 -1 10.5 | ||
| 2 1 12.0 | ||
|
|
||
| >>> def n_between(ser, low, high, **kwargs): | ||
| ... return ser.between(low, high, **kwargs).sum() | ||
|
|
||
| >>> agg_between = pd.NamedAgg("a", n_between, 0, 1) | ||
| >>> df.groupby("key").agg(count_between=agg_between) | ||
| count_between | ||
| key | ||
| 1 1 | ||
| 2 1 | ||
|
|
||
| >>> agg_between_kw = pd.NamedAgg("a", n_between, 0, 1, inclusive="both") | ||
| >>> df.groupby("key").agg(count_between_kw=agg_between_kw) | ||
| count_between_kw | ||
| key | ||
| 1 1 | ||
| 2 1 | ||
| """ | ||
|
|
||
| column: Hashable | ||
| aggfunc: AggScalar | ||
|
|
||
| __slots__ = () | ||
|
|
||
| def __new__( | ||
| cls, | ||
| column: Hashable, | ||
| aggfunc: Callable[..., Any] | str, | ||
| *args: Any, | ||
| **kwargs: Any, | ||
| ) -> Self: | ||
| if ( | ||
| callable(aggfunc) | ||
| and not getattr(aggfunc, "_is_wrapped", False) | ||
| and (args or kwargs) | ||
| ): | ||
| original_func = aggfunc | ||
|
|
||
| def wrapped(*call_args: Any, **call_kwargs: Any) -> Any: | ||
| series = call_args[0] | ||
| final_args = call_args[1:] + args | ||
| final_kwargs = {**kwargs, **call_kwargs} | ||
| return original_func(series, *final_args, **final_kwargs) | ||
|
|
||
| wrapped._is_wrapped = True # type: ignore[attr-defined] | ||
| aggfunc = wrapped | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In line with the above, this changes the |
||
| return super().__new__(cls, (column, aggfunc)) | ||
|
|
||
|
|
||
| @set_module("pandas.api.typing") | ||
| class SeriesGroupBy(GroupBy[Series]): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems too breaking. Previously, users could access
NamedAgg.columnafter creation, but not if we inherit from tuple. Can we use a dataclass here instead:We could then possibly deprecate
__getitem__access.