Skip to content

Commit 926ae91

Browse files
committed
Moved fixes into '_groupby_and_aggregate'.
1 parent f90d3e7 commit 926ae91

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

pandas/core/resample.py

+14-25
Original file line numberDiff line numberDiff line change
@@ -387,42 +387,31 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
387387

388388
obj = self._selected_obj
389389

390+
result = None
390391
try:
391392
grouped = groupby(obj, by=None, grouper=grouper, axis=self.axis)
392393
except TypeError:
393394

394395
# panel grouper
395396
grouped = PanelGroupBy(obj, grouper=grouper, axis=self.axis)
397+
result = grouped.aggregate(how, *args, **kwargs)
396398

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)
404411

405412
result = self._apply_loffset(result)
406413
return self._wrap_result(result)
407414

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-
426415
def _apply_loffset(self, result):
427416
"""
428417
if loffset is set, offset the result index

0 commit comments

Comments
 (0)