@@ -387,42 +387,31 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
387
387
388
388
obj = self ._selected_obj
389
389
390
+ result = None
390
391
try :
391
392
grouped = groupby (obj , by = None , grouper = grouper , axis = self .axis )
392
393
except TypeError :
393
394
394
395
# panel grouper
395
396
grouped = PanelGroupBy (obj , grouper = grouper , axis = self .axis )
397
+ result = grouped .aggregate (how , * args , ** kwargs )
396
398
397
- try :
398
- result = self ._try_aggregate (grouped , how , * args , ** kwargs )
399
- except Exception :
400
-
401
- # we have a non-reducing function
402
- # try to evaluate
403
- result = grouped .apply (how , * args , ** kwargs )
399
+ if result is None :
400
+ try :
401
+ if compat .callable (how ):
402
+ # Check if the function is reducing or not.
403
+ result = grouped ._aggregate_item_by_item (how , * args , ** kwargs )
404
+ else :
405
+ result = grouped .aggregate (how , * args , ** kwargs )
406
+ except Exception :
407
+
408
+ # we have a non-reducing function
409
+ # try to evaluate
410
+ result = grouped .apply (how , * args , ** kwargs )
404
411
405
412
result = self ._apply_loffset (result )
406
413
return self ._wrap_result (result )
407
414
408
- def _try_aggregate (self , grouped , how , * args , ** kwargs ):
409
- """
410
- Tries to aggregate a 'grouped' object.
411
- When 'how' param is a callable, we aggregate item by item
412
- to check if the function is reducing or not.
413
-
414
- Parameters
415
- ----------
416
- grouped : GroupBy object
417
- how : string / cython mapped function
418
- """
419
- if not compat .callable (how ) or isinstance (grouped , PanelGroupBy ):
420
- return grouped .aggregate (how , * args , ** kwargs )
421
-
422
- # Callables might change the count of columns (GH #15169)
423
- result = grouped ._aggregate_item_by_item (how , * args , ** kwargs )
424
- return result
425
-
426
415
def _apply_loffset (self , result ):
427
416
"""
428
417
if loffset is set, offset the result index
0 commit comments