Skip to content

REF: Move the rest of Series.agg into apply #39954

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 1 commit into from
Feb 21, 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
30 changes: 30 additions & 0 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,36 @@ def apply(self) -> FrameOrSeriesUnion:

return self.apply_standard()

def agg(self):
result = super().agg()
if result is None:
f = self.f
args = self.args
kwargs = self.kwargs

# string, list-like, and dict-like are entirely handled in super
assert callable(f)

# we can be called from an inner function which
# passes this meta-data
kwargs.pop("_axis", None)
kwargs.pop("_level", None)

# try a regular apply, this evaluates lambdas
# row-by-row; however if the lambda is expected a Series
# expression, e.g.: lambda x: x-x.quantile(0.25)
# this will fail, so we can try a vectorized evaluation

# we cannot FIRST try the vectorized evaluation, because
# then .agg and .apply would have different semantics if the
# operation is actually defined on the Series, e.g. str
try:
result = self.obj.apply(f, *args, **kwargs)
except (ValueError, AttributeError, TypeError):
result = f(self.obj, *args, **kwargs)

return result

def apply_empty_result(self) -> Series:
obj = self.obj
return obj._constructor(dtype=obj.dtype, index=obj.index).__finalize__(
Expand Down
23 changes: 1 addition & 22 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4003,26 +4003,6 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):

op = series_apply(self, func, args=args, kwargs=kwargs)
result = op.agg()
if result is None:

# we can be called from an inner function which
# passes this meta-data
kwargs.pop("_axis", None)
kwargs.pop("_level", None)

# try a regular apply, this evaluates lambdas
# row-by-row; however if the lambda is expected a Series
# expression, e.g.: lambda x: x-x.quantile(0.25)
# this will fail, so we can try a vectorized evaluation

# we cannot FIRST try the vectorized evaluation, because
# then .agg and .apply would have different semantics if the
# operation is actually defined on the Series, e.g. str
try:
result = self.apply(func, *args, **kwargs)
except (ValueError, AttributeError, TypeError):
result = func(self, *args, **kwargs)

return result

agg = aggregate
Expand Down Expand Up @@ -4146,8 +4126,7 @@ def apply(
Helsinki 2.484907
dtype: float64
"""
op = series_apply(self, func, convert_dtype, args, kwargs)
return op.apply()
return series_apply(self, func, convert_dtype, args, kwargs).apply()

def _reduce(
self,
Expand Down