Skip to content

Commit bf31b35

Browse files
committed
code impl and examples
1 parent 3842aa5 commit bf31b35

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

pandas/core/groupby/generic.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,57 @@
113113

114114

115115
@set_module("pandas")
116+
<<<<<<< HEAD
116117
@dataclasses.dataclass
117118
class NamedAgg:
119+
=======
120+
class NamedAgg(tuple):
121+
__slots__ = ()
122+
123+
def __new__(cls, column, aggfunc, *args, **kwargs):
124+
if (
125+
callable(aggfunc)
126+
and not getattr(aggfunc, "_is_wrapped", False)
127+
and (args or kwargs)
128+
):
129+
original_func = aggfunc
130+
131+
def wrapped(*call_args, **call_kwargs):
132+
series = call_args[0]
133+
final_args = call_args[1:] + args
134+
final_kwargs = {**kwargs, **call_kwargs}
135+
return original_func(series, *final_args, **final_kwargs)
136+
137+
wrapped._is_wrapped = True
138+
aggfunc = wrapped
139+
return super().__new__(cls, (column, aggfunc))
140+
141+
>>>>>>> 4e8ccf6958 (code impl and examples)
118142
"""
119-
Helper for column specific aggregation with control over output column names.
143+
Helper for column specific aggregation with with flexible argument passing and
144+
control over output column names.
145+
146+
<<<<<<< HEAD
147+
=======
148+
Subclass of tuple that wraps an aggregation function.
120149
150+
>>>>>>> 4e8ccf6958 (code impl and examples)
121151
Parameters
122152
----------
123153
column : Hashable
124154
Column label in the DataFrame to apply aggfunc.
125155
aggfunc : function or str
126156
Function to apply to the provided column. If string, the name of a built-in
127157
pandas function.
158+
<<<<<<< HEAD
128159
*args, **kwargs :
129160
Optional positional and keyword arguments passed to ``aggfunc``.
161+
=======
162+
*args : tuple, optional
163+
Positional arguments to pass to `aggfunc` when it is called.
164+
**kwargs : dict, optional
165+
Keyword arguments to pass to `aggfunc` when it is called.
166+
>>>>>>> 4e8ccf6958 (code impl and examples)
130167
131168
See Also
132169
--------
@@ -143,19 +180,36 @@ class NamedAgg:
143180
1 -1 10.5
144181
2 1 12.0
145182
183+
<<<<<<< HEAD
146184
>>> def n_between(ser, low, high, **kwargs):
147185
... return ser.between(low, high, **kwargs).sum()
148186
149187
>>> agg_between = pd.NamedAgg("a", n_between, 0, 1)
150188
>>> df.groupby("key").agg(count_between=agg_between)
151189
count_between
190+
=======
191+
def n_between(ser, low, high, **kwargs):
192+
return ser.between(low, high, **kwargs).sum()
193+
194+
Using positional arguments
195+
agg_between = pd.NamedAgg("a", n_between, 0, 1)
196+
df.groupby("key").agg(count_between=agg_between)
197+
count_between
198+
>>>>>>> 4e8ccf6958 (code impl and examples)
152199
key
153200
1 1
154201
2 1
155202
203+
<<<<<<< HEAD
156204
>>> agg_between_kw = pd.NamedAgg("a", n_between, 0, 1, inclusive="both")
157205
>>> df.groupby("key").agg(count_between_kw=agg_between_kw)
158206
count_between_kw
207+
=======
208+
Using both positional and keyword arguments
209+
agg_between_kw = pd.NamedAgg("a", n_between, 0, 1, inclusive="both")
210+
df.groupby("key").agg(count_between_kw=agg_between_kw)
211+
count_between_kw
212+
>>>>>>> 4e8ccf6958 (code impl and examples)
159213
key
160214
1 1
161215
2 1

0 commit comments

Comments
 (0)