Skip to content

Commit f41d59c

Browse files
jbrockmendelyeshsurya
authored andcommitted
CLN: avoid catching AttributeErorr in DataFrameGroupBy.aggreate (pandas-dev#41103)
1 parent 5cd4ab8 commit f41d59c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

pandas/core/groupby/generic.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -1024,24 +1024,28 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
10241024
else:
10251025

10261026
# try to treat as if we are passing a list
1027+
gba = GroupByApply(self, [func], args=(), kwargs={})
10271028
try:
1028-
result = GroupByApply(self, [func], args=(), kwargs={}).agg()
1029-
1030-
# select everything except for the last level, which is the one
1031-
# containing the name of the function(s), see GH 32040
1032-
result.columns = result.columns.rename(
1033-
[self._selected_obj.columns.name] * result.columns.nlevels
1034-
).droplevel(-1)
1029+
result = gba.agg()
10351030

10361031
except ValueError as err:
10371032
if "no results" not in str(err):
10381033
# raised directly by _aggregate_multiple_funcs
10391034
raise
10401035
result = self._aggregate_frame(func)
1041-
except AttributeError:
1042-
# catch exception from line 969
1043-
# (Series does not have attribute "columns"), see GH 35246
1044-
result = self._aggregate_frame(func)
1036+
1037+
else:
1038+
sobj = self._selected_obj
1039+
1040+
if isinstance(sobj, Series):
1041+
# GH#35246 test_groupby_as_index_select_column_sum_empty_df
1042+
result.columns = [self._selected_obj.name]
1043+
else:
1044+
# select everything except for the last level, which is the one
1045+
# containing the name of the function(s), see GH#32040
1046+
result.columns = result.columns.rename(
1047+
[sobj.columns.name] * result.columns.nlevels
1048+
).droplevel(-1)
10451049

10461050
if relabeling:
10471051

0 commit comments

Comments
 (0)