Skip to content

Commit 1b6b581

Browse files
authored
REF: Move the rest of DataFrame.agg into apply (#39955)
1 parent fad5fee commit 1b6b581

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

pandas/core/apply.py

+30
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def f(x):
151151
else:
152152
f = func
153153

154+
self.orig_f: AggFuncType = func
154155
self.f: AggFuncType = f
155156

156157
@property
@@ -527,6 +528,35 @@ def apply(self) -> FrameOrSeriesUnion:
527528

528529
return self.apply_standard()
529530

531+
def agg(self):
532+
obj = self.obj
533+
axis = self.axis
534+
535+
# TODO: Avoid having to change state
536+
self.obj = self.obj if self.axis == 0 else self.obj.T
537+
self.axis = 0
538+
539+
result = None
540+
try:
541+
result = super().agg()
542+
except TypeError as err:
543+
exc = TypeError(
544+
"DataFrame constructor called with "
545+
f"incompatible data and dtype: {err}"
546+
)
547+
raise exc from err
548+
finally:
549+
self.obj = obj
550+
self.axis = axis
551+
552+
if axis == 1:
553+
result = result.T if result is not None else result
554+
555+
if result is None:
556+
result = self.obj.apply(self.orig_f, axis, args=self.args, **self.kwargs)
557+
558+
return result
559+
530560
def apply_empty_result(self):
531561
"""
532562
we have an empty result; at least 1 axis is 0

pandas/core/frame.py

+4-30
Original file line numberDiff line numberDiff line change
@@ -7728,21 +7728,14 @@ def _gotitem(
77287728
examples=_agg_examples_doc,
77297729
)
77307730
def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
7731+
from pandas.core.apply import frame_apply
7732+
77317733
axis = self._get_axis_number(axis)
77327734

77337735
relabeling, func, columns, order = reconstruct_func(func, **kwargs)
77347736

7735-
result = None
7736-
try:
7737-
result = self._aggregate(func, axis, *args, **kwargs)
7738-
except TypeError as err:
7739-
exc = TypeError(
7740-
"DataFrame constructor called with "
7741-
f"incompatible data and dtype: {err}"
7742-
)
7743-
raise exc from err
7744-
if result is None:
7745-
return self.apply(func, axis=axis, args=args, **kwargs)
7737+
op = frame_apply(self, func=func, axis=axis, args=args, kwargs=kwargs)
7738+
result = op.agg()
77467739

77477740
if relabeling:
77487741
# This is to keep the order to columns occurrence unchanged, and also
@@ -7758,25 +7751,6 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
77587751

77597752
return result
77607753

7761-
def _aggregate(self, arg, axis: Axis = 0, *args, **kwargs):
7762-
from pandas.core.apply import frame_apply
7763-
7764-
op = frame_apply(
7765-
self if axis == 0 else self.T,
7766-
func=arg,
7767-
axis=0,
7768-
args=args,
7769-
kwargs=kwargs,
7770-
)
7771-
result = op.agg()
7772-
7773-
if axis == 1:
7774-
# NDFrame.aggregate returns a tuple, and we need to transpose
7775-
# only result
7776-
result = result.T if result is not None else result
7777-
7778-
return result
7779-
77807754
agg = aggregate
77817755

77827756
@doc(

0 commit comments

Comments
 (0)