Skip to content

Commit fad5fee

Browse files
authored
CLN: Move the rest of Series.apply into apply (#39954)
1 parent 8051248 commit fad5fee

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

pandas/core/apply.py

+30
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,36 @@ def apply(self) -> FrameOrSeriesUnion:
845845

846846
return self.apply_standard()
847847

848+
def agg(self):
849+
result = super().agg()
850+
if result is None:
851+
f = self.f
852+
args = self.args
853+
kwargs = self.kwargs
854+
855+
# string, list-like, and dict-like are entirely handled in super
856+
assert callable(f)
857+
858+
# we can be called from an inner function which
859+
# passes this meta-data
860+
kwargs.pop("_axis", None)
861+
kwargs.pop("_level", None)
862+
863+
# try a regular apply, this evaluates lambdas
864+
# row-by-row; however if the lambda is expected a Series
865+
# expression, e.g.: lambda x: x-x.quantile(0.25)
866+
# this will fail, so we can try a vectorized evaluation
867+
868+
# we cannot FIRST try the vectorized evaluation, because
869+
# then .agg and .apply would have different semantics if the
870+
# operation is actually defined on the Series, e.g. str
871+
try:
872+
result = self.obj.apply(f, *args, **kwargs)
873+
except (ValueError, AttributeError, TypeError):
874+
result = f(self.obj, *args, **kwargs)
875+
876+
return result
877+
848878
def apply_empty_result(self) -> Series:
849879
obj = self.obj
850880
return obj._constructor(dtype=obj.dtype, index=obj.index).__finalize__(

pandas/core/series.py

+1-22
Original file line numberDiff line numberDiff line change
@@ -4003,26 +4003,6 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):
40034003

40044004
op = series_apply(self, func, args=args, kwargs=kwargs)
40054005
result = op.agg()
4006-
if result is None:
4007-
4008-
# we can be called from an inner function which
4009-
# passes this meta-data
4010-
kwargs.pop("_axis", None)
4011-
kwargs.pop("_level", None)
4012-
4013-
# try a regular apply, this evaluates lambdas
4014-
# row-by-row; however if the lambda is expected a Series
4015-
# expression, e.g.: lambda x: x-x.quantile(0.25)
4016-
# this will fail, so we can try a vectorized evaluation
4017-
4018-
# we cannot FIRST try the vectorized evaluation, because
4019-
# then .agg and .apply would have different semantics if the
4020-
# operation is actually defined on the Series, e.g. str
4021-
try:
4022-
result = self.apply(func, *args, **kwargs)
4023-
except (ValueError, AttributeError, TypeError):
4024-
result = func(self, *args, **kwargs)
4025-
40264006
return result
40274007

40284008
agg = aggregate
@@ -4146,8 +4126,7 @@ def apply(
41464126
Helsinki 2.484907
41474127
dtype: float64
41484128
"""
4149-
op = series_apply(self, func, convert_dtype, args, kwargs)
4150-
return op.apply()
4129+
return series_apply(self, func, convert_dtype, args, kwargs).apply()
41514130

41524131
def _reduce(
41534132
self,

0 commit comments

Comments
 (0)