Skip to content

Commit 287a984

Browse files
committed
Added applying of multiple columns to resample
1 parent 5b99ae2 commit 287a984

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,7 @@ Groupby/Resample/Rolling
10231023
- Bug in ``DataFrame.groupby`` where a single level selection from a ``MultiIndex`` unexpectedly sorts (:issue:`17537`)
10241024
- Bug in ``DataFrame.groupby`` where spurious warning is raised when ``Grouper`` object is used to override ambiguous column name (:issue:`17383`)
10251025
- Bug in ``TimeGrouper`` differs when passes as a list and as a scalar (:issue:`17530`)
1026+
- Bug in ``DataFrame.resample(...).apply(...)`` when there is a callable that returns different columns (:issue:`15169`)
10261027

10271028
Sparse
10281029
^^^^^^

pandas/core/groupby.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4223,8 +4223,7 @@ def _wrap_generic_output(self, result, obj):
42234223
result_index = self.grouper.levels[0]
42244224

42254225
if self.axis == 0:
4226-
return DataFrame(result, index=obj.columns,
4227-
columns=result_index).T
4226+
return DataFrame(result, columns=result_index).T
42284227
else:
42294228
return DataFrame(result, index=obj.index,
42304229
columns=result_index)

pandas/tests/test_resample.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,20 @@ def f(x):
31033103
result = g.apply(f)
31043104
assert_frame_equal(result, expected)
31053105

3106+
def test_apply_with_mutated_index(self):
3107+
index = pd.date_range('1-1-2015', '12-31-15', freq='D')
3108+
df = pd.DataFrame(data={'col1': np.random.rand(len(index))},
3109+
index=index)
3110+
3111+
def f(x):
3112+
s = pd.Series([1, 2], index=['a', 'b'])
3113+
return s
3114+
3115+
result = df.resample('M').apply(f)
3116+
expected = df.groupby(pd.Grouper(freq='M')).apply(f)
3117+
3118+
assert_frame_equal(result, expected, check_dtype=False)
3119+
31063120
def test_resample_groupby_with_label(self):
31073121
# GH 13235
31083122
index = date_range('2000-01-01', freq='2D', periods=5)

0 commit comments

Comments
 (0)