|
114 | 114 |
|
115 | 115 | @set_module("pandas") |
116 | 116 | class NamedAgg(tuple): |
117 | | - __slots__ = () |
118 | | - |
119 | | - def __new__( |
120 | | - cls, |
121 | | - column: Hashable, |
122 | | - aggfunc: Callable[..., Any] | str, |
123 | | - *args: Any, |
124 | | - **kwargs: Any, |
125 | | - ) -> Self: |
126 | | - if ( |
127 | | - callable(aggfunc) |
128 | | - and not getattr(aggfunc, "_is_wrapped", False) |
129 | | - and (args or kwargs) |
130 | | - ): |
131 | | - original_func = aggfunc |
132 | | - |
133 | | - def wrapped(*call_args: Any, **call_kwargs: Any) -> Any: |
134 | | - series = call_args[0] |
135 | | - final_args = call_args[1:] + args |
136 | | - final_kwargs = {**kwargs, **call_kwargs} |
137 | | - return original_func(series, *final_args, **final_kwargs) |
138 | | - |
139 | | - wrapped._is_wrapped = True # type: ignore[attr-defined] |
140 | | - aggfunc = wrapped |
141 | | - return super().__new__(cls, (column, aggfunc)) |
142 | | - |
143 | 117 | """ |
144 | 118 | Helper for column specific aggregation with with flexible argument passing and |
145 | 119 | control over output column names. |
@@ -196,6 +170,32 @@ def n_between(ser, low, high, **kwargs): |
196 | 170 | column: Hashable |
197 | 171 | aggfunc: AggScalar |
198 | 172 |
|
| 173 | + __slots__ = () |
| 174 | + |
| 175 | + def __new__( |
| 176 | + cls, |
| 177 | + column: Hashable, |
| 178 | + aggfunc: Callable[..., Any] | str, |
| 179 | + *args: Any, |
| 180 | + **kwargs: Any, |
| 181 | + ) -> Self: |
| 182 | + if ( |
| 183 | + callable(aggfunc) |
| 184 | + and not getattr(aggfunc, "_is_wrapped", False) |
| 185 | + and (args or kwargs) |
| 186 | + ): |
| 187 | + original_func = aggfunc |
| 188 | + |
| 189 | + def wrapped(*call_args: Any, **call_kwargs: Any) -> Any: |
| 190 | + series = call_args[0] |
| 191 | + final_args = call_args[1:] + args |
| 192 | + final_kwargs = {**kwargs, **call_kwargs} |
| 193 | + return original_func(series, *final_args, **final_kwargs) |
| 194 | + |
| 195 | + wrapped._is_wrapped = True # type: ignore[attr-defined] |
| 196 | + aggfunc = wrapped |
| 197 | + return super().__new__(cls, (column, aggfunc)) |
| 198 | + |
199 | 199 |
|
200 | 200 | @set_module("pandas.api.typing") |
201 | 201 | class SeriesGroupBy(GroupBy[Series]): |
|
0 commit comments