diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 828b460f84ec6..6e48d4b699977 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -147,18 +147,14 @@ def index(self) -> Index: def apply(self) -> FrameOrSeriesUnion: pass - def agg(self) -> Tuple[Optional[FrameOrSeriesUnion], Optional[bool]]: + def agg(self) -> Optional[FrameOrSeriesUnion]: """ Provide an implementation for the aggregators. Returns ------- - tuple of result, how. - - Notes - ----- - how can be a string describe the required post-processing, or - None if not required. + Result of aggregation, or None if agg cannot be performed by + this method. """ obj = self.obj arg = self.f @@ -171,23 +167,21 @@ def agg(self) -> Tuple[Optional[FrameOrSeriesUnion], Optional[bool]]: result = self.maybe_apply_str() if result is not None: - return result, None + return result if is_dict_like(arg): - return self.agg_dict_like(_axis), True + return self.agg_dict_like(_axis) elif is_list_like(arg): # we require a list, but not a 'str' - return self.agg_list_like(_axis=_axis), None - else: - result = None + return self.agg_list_like(_axis=_axis) if callable(arg): f = obj._get_cython_func(arg) if f and not args and not kwargs: - return getattr(obj, f)(), None + return getattr(obj, f)() # caller can react - return result, True + return None def agg_list_like(self, _axis: int) -> FrameOrSeriesUnion: """ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 63d238da12101..6b44776aeb9ef 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7686,7 +7686,7 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs): result = None try: - result, how = self._aggregate(func, axis, *args, **kwargs) + result = self._aggregate(func, axis, *args, **kwargs) except TypeError as err: exc = TypeError( "DataFrame constructor called with " @@ -7720,14 +7720,14 @@ def _aggregate(self, arg, axis: Axis = 0, *args, **kwargs): args=args, kwargs=kwargs, ) - result, how = op.agg() + result = op.agg() if axis == 1: # NDFrame.aggregate returns a tuple, and we need to transpose # only result result = result.T if result is not None else result - return result, how + return result agg = aggregate diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index a7297923f1034..dd8f0e5c0b777 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -46,6 +46,7 @@ ensure_platform_int, is_bool, is_categorical_dtype, + is_dict_like, is_integer_dtype, is_interval_dtype, is_numeric_dtype, @@ -962,8 +963,8 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs) func = maybe_mangle_lambdas(func) op = GroupByApply(self, func, args, kwargs) - result, how = op.agg() - if how is None: + result = op.agg() + if not is_dict_like(func) and result is not None: return result if result is None: @@ -982,7 +983,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs) # try to treat as if we are passing a list try: - result, _ = GroupByApply( + result = GroupByApply( self, [func], args=(), kwargs={"_axis": self.axis} ).agg() diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 68f791ac0a837..2cfb13da7dd45 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -301,7 +301,7 @@ def pipe( def aggregate(self, func, *args, **kwargs): self._set_binner() - result, how = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg() + result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg() if result is None: how = func grouper = None diff --git a/pandas/core/series.py b/pandas/core/series.py index 7d97c9f6189f3..f5a0dbc996c42 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3973,7 +3973,7 @@ def aggregate(self, func=None, axis=0, *args, **kwargs): func = dict(kwargs.items()) op = series_apply(self, func, args=args, kwargs=kwargs) - result, how = op.agg() + result = op.agg() if result is None: # we can be called from an inner function which diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index b5714dbcd9e91..1538975b260c0 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -510,7 +510,7 @@ def calc(x): return self._apply_tablewise(homogeneous_func, name) def aggregate(self, func, *args, **kwargs): - result, how = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg() + result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg() if result is None: return self.apply(func, raw=False, args=args, kwargs=kwargs) return result @@ -994,7 +994,7 @@ def calc(x): axis="", ) def aggregate(self, func, *args, **kwargs): - result, how = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg() + result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg() if result is None: # these must apply directly