From b1b9e72a49492d9e1ae3ac9a40d777148e23a592 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Tue, 23 May 2023 16:19:07 +0100 Subject: [PATCH 1/2] CLN: Apply.agg_list_like --- pandas/core/apply.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 020aa4e8916da..3a0f2117c359d 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -324,6 +324,7 @@ def agg_list_like(self) -> DataFrame | Series: keys = [] is_groupby = isinstance(obj, (DataFrameGroupBy, SeriesGroupBy)) + is_ser_or_df = isinstance(obj, (ABCDataFrame, ABCSeries)) context_manager: ContextManager if is_groupby: # When as_index=False, we combine all results using indices @@ -336,12 +337,8 @@ def agg_list_like(self) -> DataFrame | Series: if selected_obj.ndim == 1: for a in arg: colg = obj._gotitem(selected_obj.name, ndim=1, subset=selected_obj) - if isinstance(colg, (ABCSeries, ABCDataFrame)): - new_res = colg.aggregate( - a, self.axis, *self.args, **self.kwargs - ) - else: - new_res = colg.aggregate(a, *self.args, **self.kwargs) + this_args = [self.axis, *self.args] if is_ser_or_df else self.args + new_res = colg.aggregate(a, *this_args, **self.kwargs) results.append(new_res) # make sure we find a good name @@ -352,12 +349,8 @@ def agg_list_like(self) -> DataFrame | Series: indices = [] for index, col in enumerate(selected_obj): colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index]) - if isinstance(colg, (ABCSeries, ABCDataFrame)): - new_res = colg.aggregate( - arg, self.axis, *self.args, **self.kwargs - ) - else: - new_res = colg.aggregate(arg, *self.args, **self.kwargs) + this_args = [self.axis, *self.args] if is_ser_or_df else self.args + new_res = colg.aggregate(arg, *this_args, **self.kwargs) results.append(new_res) indices.append(index) keys = selected_obj.columns.take(indices) From cfb5a229c0fe533c2dc09521ff0cbf8e98393233 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Tue, 23 May 2023 17:31:05 +0100 Subject: [PATCH 2/2] some cleanups --- pandas/core/apply.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 3a0f2117c359d..481217a7fb4af 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -325,6 +325,8 @@ def agg_list_like(self) -> DataFrame | Series: is_groupby = isinstance(obj, (DataFrameGroupBy, SeriesGroupBy)) is_ser_or_df = isinstance(obj, (ABCDataFrame, ABCSeries)) + this_args = [self.axis, *self.args] if is_ser_or_df else self.args + context_manager: ContextManager if is_groupby: # When as_index=False, we combine all results using indices @@ -337,7 +339,6 @@ def agg_list_like(self) -> DataFrame | Series: if selected_obj.ndim == 1: for a in arg: colg = obj._gotitem(selected_obj.name, ndim=1, subset=selected_obj) - this_args = [self.axis, *self.args] if is_ser_or_df else self.args new_res = colg.aggregate(a, *this_args, **self.kwargs) results.append(new_res) @@ -349,7 +350,6 @@ def agg_list_like(self) -> DataFrame | Series: indices = [] for index, col in enumerate(selected_obj): colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index]) - this_args = [self.axis, *self.args] if is_ser_or_df else self.args new_res = colg.aggregate(arg, *this_args, **self.kwargs) results.append(new_res) indices.append(index)