@@ -266,7 +266,11 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
266
266
func = maybe_mangle_lambdas (func )
267
267
ret = self ._aggregate_multiple_funcs (func )
268
268
if relabeling :
269
- ret .columns = columns
269
+ # error: Incompatible types in assignment (expression has type
270
+ # "Optional[List[str]]", variable has type "Index")
271
+ ret .columns = columns # type: ignore[assignment]
272
+ return ret
273
+
270
274
else :
271
275
cyfunc = com .get_cython_func (func )
272
276
if cyfunc and not args and not kwargs :
@@ -282,33 +286,21 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
282
286
# see test_groupby.test_basic
283
287
result = self ._aggregate_named (func , * args , ** kwargs )
284
288
285
- index = Index (sorted (result ), name = self .grouper .names [0 ])
286
- ret = create_series_with_explicit_dtype (
287
- result , index = index , dtype_if_empty = object
288
- )
289
-
290
- if not self .as_index : # pragma: no cover
291
- print ("Warning, ignoring as_index=True" )
292
-
293
- if isinstance (ret , dict ):
294
- from pandas import concat
295
-
296
- ret = concat (ret .values (), axis = 1 , keys = [key .label for key in ret .keys ()])
297
- return ret
289
+ index = Index (sorted (result ), name = self .grouper .names [0 ])
290
+ return create_series_with_explicit_dtype (
291
+ result , index = index , dtype_if_empty = object
292
+ )
298
293
299
294
agg = aggregate
300
295
301
- def _aggregate_multiple_funcs (self , arg ):
296
+ def _aggregate_multiple_funcs (self , arg ) -> DataFrame :
302
297
if isinstance (arg , dict ):
303
298
304
299
# show the deprecation, but only if we
305
300
# have not shown a higher level one
306
301
# GH 15931
307
- if isinstance (self ._selected_obj , Series ):
308
- raise SpecificationError ("nested renamer is not supported" )
302
+ raise SpecificationError ("nested renamer is not supported" )
309
303
310
- columns = list (arg .keys ())
311
- arg = arg .items ()
312
304
elif any (isinstance (x , (tuple , list )) for x in arg ):
313
305
arg = [(x , x ) if not isinstance (x , (tuple , list )) else x for x in arg ]
314
306
@@ -335,8 +327,14 @@ def _aggregate_multiple_funcs(self, arg):
335
327
results [base .OutputKey (label = name , position = idx )] = obj .aggregate (func )
336
328
337
329
if any (isinstance (x , DataFrame ) for x in results .values ()):
338
- # let higher level handle
339
- return results
330
+ from pandas import concat
331
+
332
+ res_df = concat (
333
+ results .values (), axis = 1 , keys = [key .label for key in results .keys ()]
334
+ )
335
+ # error: Incompatible return value type (got "Union[DataFrame, Series]",
336
+ # expected "DataFrame")
337
+ return res_df # type: ignore[return-value]
340
338
341
339
indexed_output = {key .position : val for key , val in results .items ()}
342
340
output = self .obj ._constructor_expanddim (indexed_output , index = None )
@@ -1000,6 +998,11 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
1000
998
result = op .agg ()
1001
999
if not is_dict_like (func ) and result is not None :
1002
1000
return result
1001
+ elif relabeling and result is not None :
1002
+ # this should be the only (non-raising) case with relabeling
1003
+ # used reordered index of columns
1004
+ result = result .iloc [:, order ]
1005
+ result .columns = columns
1003
1006
1004
1007
if result is None :
1005
1008
@@ -1039,12 +1042,6 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
1039
1042
[sobj .columns .name ] * result .columns .nlevels
1040
1043
).droplevel (- 1 )
1041
1044
1042
- if relabeling :
1043
-
1044
- # used reordered index of columns
1045
- result = result .iloc [:, order ]
1046
- result .columns = columns
1047
-
1048
1045
if not self .as_index :
1049
1046
self ._insert_inaxis_grouper_inplace (result )
1050
1047
result .index = np .arange (len (result ))
@@ -1389,9 +1386,7 @@ def _transform_item_by_item(self, obj: DataFrame, wrapper) -> DataFrame:
1389
1386
if not output :
1390
1387
raise TypeError ("Transform function invalid for data types" )
1391
1388
1392
- columns = obj .columns
1393
- if len (output ) < len (obj .columns ):
1394
- columns = columns .take (inds )
1389
+ columns = obj .columns .take (inds )
1395
1390
1396
1391
return self .obj ._constructor (output , index = obj .index , columns = columns )
1397
1392
0 commit comments