Skip to content

CLN: avoid catching AttributeErorr in DataFrameGroupBy.aggregate #41103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,24 +1035,28 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
else:

# try to treat as if we are passing a list
gba = GroupByApply(self, [func], args=(), kwargs={})
try:
result = GroupByApply(self, [func], args=(), kwargs={}).agg()

# select everything except for the last level, which is the one
# containing the name of the function(s), see GH 32040
result.columns = result.columns.rename(
[self._selected_obj.columns.name] * result.columns.nlevels
).droplevel(-1)
result = gba.agg()

except ValueError as err:
if "no results" not in str(err):
# raised directly by _aggregate_multiple_funcs
raise
result = self._aggregate_frame(func)
except AttributeError:
# catch exception from line 969
# (Series does not have attribute "columns"), see GH 35246
result = self._aggregate_frame(func)

else:
sobj = self._selected_obj

if isinstance(sobj, Series):
# GH#35246 test_groupby_as_index_select_column_sum_empty_df
result.columns = [self._selected_obj.name]
else:
# select everything except for the last level, which is the one
# containing the name of the function(s), see GH#32040
result.columns = result.columns.rename(
[sobj.columns.name] * result.columns.nlevels
).droplevel(-1)

if relabeling:

Expand Down