@@ -1024,24 +1024,28 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
1024
1024
else :
1025
1025
1026
1026
# try to treat as if we are passing a list
1027
+ gba = GroupByApply (self , [func ], args = (), kwargs = {})
1027
1028
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 ()
1035
1030
1036
1031
except ValueError as err :
1037
1032
if "no results" not in str (err ):
1038
1033
# raised directly by _aggregate_multiple_funcs
1039
1034
raise
1040
1035
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 )
1045
1049
1046
1050
if relabeling :
1047
1051
0 commit comments