Skip to content

Commit

Permalink
Added applying of multiple columns to resample (pandas-dev#17950)
Browse files Browse the repository at this point in the history
(cherry picked from commit bdeadb9)
  • Loading branch information
discort authored and TomAugspurger committed Dec 8, 2017
1 parent c5673af commit 6a4567e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Documentation Changes

Bug Fixes
~~~~~~~~~
- Bug in ``DataFrame.resample(...).apply(...)`` when there is a callable that returns different columns (:issue:`15169`)

Conversion
^^^^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
grouped = PanelGroupBy(obj, grouper=grouper, axis=self.axis)

try:
result = grouped.aggregate(how, *args, **kwargs)
if isinstance(obj, ABCDataFrame) and compat.callable(how):
# Check if the function is reducing or not.
result = grouped._aggregate_item_by_item(how, *args, **kwargs)
else:
result = grouped.aggregate(how, *args, **kwargs)
except Exception:

# we have a non-reducing function
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -3103,6 +3103,26 @@ def f(x):
result = g.apply(f)
assert_frame_equal(result, expected)

def test_apply_with_mutated_index(self):
# GH 15169
index = pd.date_range('1-1-2015', '12-31-15', freq='D')
df = pd.DataFrame(data={'col1': np.random.rand(len(index))},
index=index)

def f(x):
s = pd.Series([1, 2], index=['a', 'b'])
return s

expected = df.groupby(pd.Grouper(freq='M')).apply(f)

result = df.resample('M').apply(f)
assert_frame_equal(result, expected)

# A case for series
expected = df['col1'].groupby(pd.Grouper(freq='M')).apply(f)
result = df['col1'].resample('M').apply(f)
assert_series_equal(result, expected)

def test_resample_groupby_with_label(self):
# GH 13235
index = date_range('2000-01-01', freq='2D', periods=5)
Expand Down

0 comments on commit 6a4567e

Please sign in to comment.