From b92526b81812bbd7ba06e04e2908e49ff3baaa94 Mon Sep 17 00:00:00 2001 From: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Date: Sun, 14 Mar 2021 19:55:02 -0400 Subject: [PATCH] CLN: Don't modify state in FrameApply.agg (#40428) --- pandas/core/apply.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 57147461284fb..3a2c2d7124963 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -633,28 +633,28 @@ def agg(self): obj = self.obj axis = self.axis - # TODO: Avoid having to change state - self.obj = self.obj if self.axis == 0 else self.obj.T - self.axis = 0 - - result = None try: - result = super().agg() + if axis == 1: + result = FrameRowApply( + obj.T, + self.orig_f, + self.raw, + self.result_type, + self.args, + self.kwargs, + ).agg() + result = result.T if result is not None else result + else: + result = super().agg() except TypeError as err: exc = TypeError( "DataFrame constructor called with " f"incompatible data and dtype: {err}" ) raise exc from err - finally: - self.obj = obj - self.axis = axis - - if axis == 1: - result = result.T if result is not None else result if result is None: - result = self.obj.apply(self.orig_f, axis, args=self.args, **self.kwargs) + result = obj.apply(self.orig_f, axis, args=self.args, **self.kwargs) return result