Skip to content

REF: Move Resampler/Window.agg into core.apply #39459

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

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions pandas/core/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,56 +534,6 @@ def transform_str_or_callable(
return func(obj, *args, **kwargs)


def aggregate(
obj: AggObjType,
arg: AggFuncType,
*args,
**kwargs,
):
"""
Provide an implementation for the aggregators.

Parameters
----------
obj : Pandas object to compute aggregation on.
arg : string, dict, function.
*args : args to pass on to the function.
**kwargs : kwargs to pass on to the function.

Returns
-------
tuple of result, how.

Notes
-----
how can be a string describe the required post-processing, or
None if not required.
"""
_axis = kwargs.pop("_axis", None)
if _axis is None:
_axis = getattr(obj, "axis", 0)

if isinstance(arg, str):
return obj._try_aggregate_string_function(arg, *args, **kwargs), None
elif is_dict_like(arg):
arg = cast(AggFuncTypeDict, arg)
return agg_dict_like(obj, arg, _axis), True
elif is_list_like(arg):
# we require a list, but not an 'str'
arg = cast(List[AggFuncTypeBase], arg)
return agg_list_like(obj, arg, _axis=_axis), None
else:
result = None

if callable(arg):
f = obj._get_cython_func(arg)
if f and not args and not kwargs:
return getattr(obj, f)(), None

# caller can react
return result, True


def agg_list_like(
obj: AggObjType,
arg: List[AggFuncTypeBase],
Expand Down
26 changes: 26 additions & 0 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
if TYPE_CHECKING:
from pandas import DataFrame, Index, Series
from pandas.core.groupby import DataFrameGroupBy, SeriesGroupBy
from pandas.core.resample import Resampler
from pandas.core.window.rolling import BaseWindow

ResType = Dict[int, Any]

Expand Down Expand Up @@ -684,3 +686,27 @@ def __init__(

def apply(self):
raise NotImplementedError


class ResamplerWindowApply(Apply):
axis = 0
obj: Union[Resampler, BaseWindow]

def __init__(
self,
obj: Union[Resampler, BaseWindow],
func: AggFuncType,
args,
kwds,
):
super().__init__(
obj,
func,
raw=False,
result_type=None,
args=args,
kwds=kwds,
)

def apply(self):
raise NotImplementedError
4 changes: 2 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

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

from pandas.core.aggregation import aggregate
import pandas.core.algorithms as algos
from pandas.core.apply import ResamplerWindowApply
from pandas.core.base import DataError
from pandas.core.generic import NDFrame, _shared_docs
from pandas.core.groupby.base import GotItemMixin, ShallowMixin
Expand Down Expand Up @@ -301,7 +301,7 @@ def pipe(
def aggregate(self, func, *args, **kwargs):

self._set_binner()
result, how = aggregate(self, func, *args, **kwargs)
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
if result is None:
how = func
grouper = None
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
)
from pandas.core.dtypes.missing import notna

from pandas.core.aggregation import aggregate
from pandas.core.apply import ResamplerWindowApply
from pandas.core.base import DataError, SelectionMixin
from pandas.core.construction import extract_array
from pandas.core.groupby.base import GotItemMixin, ShallowMixin
Expand Down Expand Up @@ -479,7 +479,7 @@ def calc(x):
return self._apply_tablewise(homogeneous_func, name)

def aggregate(self, func, *args, **kwargs):
result, how = aggregate(self, func, *args, **kwargs)
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
if result is None:
return self.apply(func, raw=False, args=args, kwargs=kwargs)
return result
Expand Down Expand Up @@ -1150,7 +1150,7 @@ def calc(x):
axis="",
)
def aggregate(self, func, *args, **kwargs):
result, how = aggregate(self, func, *args, **kwargs)
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
if result is None:

# these must apply directly
Expand Down