Skip to content

Commit 89ceec6

Browse files
authored
REF: Move Resampler/Window.agg into core.apply (#39459)
1 parent 1518f35 commit 89ceec6

File tree

4 files changed

+31
-55
lines changed

4 files changed

+31
-55
lines changed

Diff for: pandas/core/aggregation.py

-50
Original file line numberDiff line numberDiff line change
@@ -534,56 +534,6 @@ def transform_str_or_callable(
534534
return func(obj, *args, **kwargs)
535535

536536

537-
def aggregate(
538-
obj: AggObjType,
539-
arg: AggFuncType,
540-
*args,
541-
**kwargs,
542-
):
543-
"""
544-
Provide an implementation for the aggregators.
545-
546-
Parameters
547-
----------
548-
obj : Pandas object to compute aggregation on.
549-
arg : string, dict, function.
550-
*args : args to pass on to the function.
551-
**kwargs : kwargs to pass on to the function.
552-
553-
Returns
554-
-------
555-
tuple of result, how.
556-
557-
Notes
558-
-----
559-
how can be a string describe the required post-processing, or
560-
None if not required.
561-
"""
562-
_axis = kwargs.pop("_axis", None)
563-
if _axis is None:
564-
_axis = getattr(obj, "axis", 0)
565-
566-
if isinstance(arg, str):
567-
return obj._try_aggregate_string_function(arg, *args, **kwargs), None
568-
elif is_dict_like(arg):
569-
arg = cast(AggFuncTypeDict, arg)
570-
return agg_dict_like(obj, arg, _axis), True
571-
elif is_list_like(arg):
572-
# we require a list, but not an 'str'
573-
arg = cast(List[AggFuncTypeBase], arg)
574-
return agg_list_like(obj, arg, _axis=_axis), None
575-
else:
576-
result = None
577-
578-
if callable(arg):
579-
f = obj._get_cython_func(arg)
580-
if f and not args and not kwargs:
581-
return getattr(obj, f)(), None
582-
583-
# caller can react
584-
return result, True
585-
586-
587537
def agg_list_like(
588538
obj: AggObjType,
589539
arg: List[AggFuncTypeBase],

Diff for: pandas/core/apply.py

+26
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
if TYPE_CHECKING:
4848
from pandas import DataFrame, Index, Series
4949
from pandas.core.groupby import DataFrameGroupBy, SeriesGroupBy
50+
from pandas.core.resample import Resampler
51+
from pandas.core.window.rolling import BaseWindow
5052

5153
ResType = Dict[int, Any]
5254

@@ -684,3 +686,27 @@ def __init__(
684686

685687
def apply(self):
686688
raise NotImplementedError
689+
690+
691+
class ResamplerWindowApply(Apply):
692+
axis = 0
693+
obj: Union[Resampler, BaseWindow]
694+
695+
def __init__(
696+
self,
697+
obj: Union[Resampler, BaseWindow],
698+
func: AggFuncType,
699+
args,
700+
kwds,
701+
):
702+
super().__init__(
703+
obj,
704+
func,
705+
raw=False,
706+
result_type=None,
707+
args=args,
708+
kwds=kwds,
709+
)
710+
711+
def apply(self):
712+
raise NotImplementedError

Diff for: pandas/core/resample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
2525

26-
from pandas.core.aggregation import aggregate
2726
import pandas.core.algorithms as algos
27+
from pandas.core.apply import ResamplerWindowApply
2828
from pandas.core.base import DataError
2929
from pandas.core.generic import NDFrame, _shared_docs
3030
from pandas.core.groupby.base import GotItemMixin, ShallowMixin
@@ -301,7 +301,7 @@ def pipe(
301301
def aggregate(self, func, *args, **kwargs):
302302

303303
self._set_binner()
304-
result, how = aggregate(self, func, *args, **kwargs)
304+
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
305305
if result is None:
306306
how = func
307307
grouper = None

Diff for: pandas/core/window/rolling.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
)
4949
from pandas.core.dtypes.missing import notna
5050

51-
from pandas.core.aggregation import aggregate
51+
from pandas.core.apply import ResamplerWindowApply
5252
from pandas.core.base import DataError, SelectionMixin
5353
from pandas.core.construction import extract_array
5454
from pandas.core.groupby.base import GotItemMixin, ShallowMixin
@@ -479,7 +479,7 @@ def calc(x):
479479
return self._apply_tablewise(homogeneous_func, name)
480480

481481
def aggregate(self, func, *args, **kwargs):
482-
result, how = aggregate(self, func, *args, **kwargs)
482+
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
483483
if result is None:
484484
return self.apply(func, raw=False, args=args, kwargs=kwargs)
485485
return result
@@ -1150,7 +1150,7 @@ def calc(x):
11501150
axis="",
11511151
)
11521152
def aggregate(self, func, *args, **kwargs):
1153-
result, how = aggregate(self, func, *args, **kwargs)
1153+
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
11541154
if result is None:
11551155

11561156
# these must apply directly

0 commit comments

Comments
 (0)