Skip to content

Commit

Permalink
CLN: Don't modify state in FrameApply.agg (#40428)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach authored Mar 14, 2021
1 parent cd5f15d commit b92526b
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b92526b

Please sign in to comment.