Skip to content

Commit

Permalink
CLN: Remove "how" return from agg (pandas-dev#39786)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach authored Feb 15, 2021
1 parent 4131b61 commit 4861e40
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
22 changes: 8 additions & 14 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
"""
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
ensure_platform_int,
is_bool,
is_categorical_dtype,
is_dict_like,
is_integer_dtype,
is_interval_dtype,
is_numeric_dtype,
Expand Down Expand Up @@ -968,8 +969,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:
Expand All @@ -988,7 +989,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()

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4861e40

Please sign in to comment.