Skip to content

Commit 5bc7f9f

Browse files
committed
Moved fixes into '_groupby_and_aggregate'.
1 parent b2ea813 commit 5bc7f9f

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

pandas/core/resample.py

+5-19
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,11 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
395395
grouped = PanelGroupBy(obj, grouper=grouper, axis=self.axis)
396396

397397
try:
398-
result = self._try_aggregate(grouped, how, *args, **kwargs)
398+
if isinstance(obj, ABCDataFrame) and compat.callable(how):
399+
# Check if the function is reducing or not.
400+
result = grouped._aggregate_item_by_item(how, *args, **kwargs)
401+
else:
402+
result = grouped.aggregate(how, *args, **kwargs)
399403
except Exception:
400404

401405
# we have a non-reducing function
@@ -405,24 +409,6 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
405409
result = self._apply_loffset(result)
406410
return self._wrap_result(result)
407411

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

pandas/tests/test_resample.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3113,11 +3113,16 @@ def f(x):
31133113
s = pd.Series([1, 2], index=['a', 'b'])
31143114
return s
31153115

3116-
result = df.resample('M').apply(f)
31173116
expected = df.groupby(pd.Grouper(freq='M')).apply(f)
31183117

3118+
result = df.resample('M').apply(f)
31193119
assert_frame_equal(result, expected)
31203120

3121+
# A case for series
3122+
expected = df['col1'].groupby(pd.Grouper(freq='M')).apply(f)
3123+
result = df['col1'].resample('M').apply(f)
3124+
assert_series_equal(result, expected)
3125+
31213126
def test_resample_groupby_with_label(self):
31223127
# GH 13235
31233128
index = date_range('2000-01-01', freq='2D', periods=5)

0 commit comments

Comments
 (0)