1717 TYPE_CHECKING ,
1818 Any ,
1919 Literal ,
20- Self ,
2120 TypeAlias ,
2221 TypeVar ,
2322 cast ,
114113
115114
116115@set_module ("pandas" )
117- < << << << HEAD
118116@dataclasses .dataclass
119117class NamedAgg :
120- == == == =
121- class NamedAgg (tuple ):
122- __slots__ = ()
123-
124- def __new__ (
125- cls ,
126- column : Hashable ,
127- aggfunc : Callable [..., Any ] | str ,
128- * args : Any ,
129- ** kwargs : Any ,
130- ) -> Self :
131- if (
132- callable (aggfunc )
133- and not getattr (aggfunc , "_is_wrapped" , False )
134- and (args or kwargs )
135- ):
136- original_func = aggfunc
137-
138- def wrapped (* call_args : Any , ** call_kwargs : Any ) -> Any :
139- series = call_args [0 ]
140- final_args = call_args [1 :] + args
141- final_kwargs = {** kwargs , ** call_kwargs }
142- return original_func (series , * final_args , ** final_kwargs )
143-
144- wrapped ._is_wrapped = True # type: ignore[attr-defined]
145- aggfunc = wrapped
146- return super ().__new__ (cls , (column , aggfunc ))
147-
148- > >> >> >> 4e8 ccf6958 (code impl and examples )
149118 """
150- Helper for column specific aggregation with with flexible argument passing and
151- control over output column names.
152-
153- <<<<<<< HEAD
154- =======
155- Subclass of tuple that wraps an aggregation function.
119+ Helper for column specific aggregation with control over output column names.
156120
157- >>>>>>> 4e8ccf6958 (code impl and examples)
158121 Parameters
159122 ----------
160123 column : Hashable
161124 Column label in the DataFrame to apply aggfunc.
162125 aggfunc : function or str
163126 Function to apply to the provided column. If string, the name of a built-in
164127 pandas function.
165- <<<<<<< HEAD
166128 *args, **kwargs :
167129 Optional positional and keyword arguments passed to ``aggfunc``.
168- =======
169- *args : tuple, optional
170- Positional arguments to pass to `aggfunc` when it is called.
171- **kwargs : dict, optional
172- Keyword arguments to pass to `aggfunc` when it is called.
173- >>>>>>> 4e8ccf6958 (code impl and examples)
174130
175131 See Also
176132 --------
@@ -187,36 +143,19 @@ def wrapped(*call_args: Any, **call_kwargs: Any) -> Any:
187143 1 -1 10.5
188144 2 1 12.0
189145
190- <<<<<<< HEAD
191146 >>> def n_between(ser, low, high, **kwargs):
192147 ... return ser.between(low, high, **kwargs).sum()
193148
194149 >>> agg_between = pd.NamedAgg("a", n_between, 0, 1)
195150 >>> df.groupby("key").agg(count_between=agg_between)
196151 count_between
197- =======
198- def n_between(ser, low, high, **kwargs):
199- return ser.between(low, high, **kwargs).sum()
200-
201- Using positional arguments
202- agg_between = pd.NamedAgg("a", n_between, 0, 1)
203- df.groupby("key").agg(count_between=agg_between)
204- count_between
205- >>>>>>> 4e8ccf6958 (code impl and examples)
206152 key
207153 1 1
208154 2 1
209155
210- <<<<<<< HEAD
211156 >>> agg_between_kw = pd.NamedAgg("a", n_between, 0, 1, inclusive="both")
212157 >>> df.groupby("key").agg(count_between_kw=agg_between_kw)
213158 count_between_kw
214- =======
215- Using both positional and keyword arguments
216- agg_between_kw = pd.NamedAgg("a", n_between, 0, 1, inclusive="both")
217- df.groupby("key").agg(count_between_kw=agg_between_kw)
218- count_between_kw
219- >>>>>>> 4e8ccf6958 (code impl and examples)
220159 key
221160 1 1
222161 2 1
0 commit comments