Skip to content

Commit 21ee836

Browse files
WillAydjreback
authored andcommitted
Consistent Return Structure for Rolling Apply (#20984)
1 parent 673fe6e commit 21ee836

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,7 @@ Groupby/Resample/Rolling
13281328
- Bug in :func:`DataFrame.groupby` where transformations using ``np.all`` and ``np.any`` were raising a ``ValueError`` (:issue:`20653`)
13291329
- Bug in :func:`DataFrame.resample` where ``ffill``, ``bfill``, ``pad``, ``backfill``, ``fillna``, ``interpolate``, and ``asfreq`` were ignoring ``loffset``. (:issue:`20744`)
13301330
- Bug in :func:`DataFrame.groupby` when applying a function that has mixed data types and the user supplied function can fail on the grouping column (:issue:`20949`)
1331+
- Bug in :func:`DataFrameGroupBy.rolling().apply() <pandas.core.window.Rolling.apply>` where operations performed against the associated :class:`DataFrameGroupBy` object could impact the inclusion of the grouped item(s) in the result (:issue:`14013`)
13311332

13321333
Sparse
13331334
^^^^^^

pandas/core/window.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,7 @@ def _apply(self, func, name=None, window=None, center=None,
837837
index, indexi = self._get_index(index=index)
838838
results = []
839839
for b in blocks:
840-
try:
841-
values = self._prep_values(b.values)
842-
except TypeError:
843-
results.append(b.values.copy())
844-
continue
840+
values = self._prep_values(b.values)
845841

846842
if values.size == 0:
847843
results.append(values.copy())

pandas/tests/test_window.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,19 @@ def test_attribute_access(self):
105105
def tests_skip_nuisance(self):
106106

107107
df = DataFrame({'A': range(5), 'B': range(5, 10), 'C': 'foo'})
108-
109108
r = df.rolling(window=3)
110109
result = r[['A', 'B']].sum()
111110
expected = DataFrame({'A': [np.nan, np.nan, 3, 6, 9],
112111
'B': [np.nan, np.nan, 18, 21, 24]},
113112
columns=list('AB'))
114113
tm.assert_frame_equal(result, expected)
115114

116-
expected = concat([r[['A', 'B']].sum(), df[['C']]], axis=1)
117-
result = r.sum()
118-
tm.assert_frame_equal(result, expected, check_like=True)
115+
def test_skip_sum_object_raises(self):
116+
df = DataFrame({'A': range(5), 'B': range(5, 10), 'C': 'foo'})
117+
r = df.rolling(window=3)
118+
119+
with tm.assert_raises_regex(TypeError, 'cannot handle this type'):
120+
r.sum()
119121

120122
def test_agg(self):
121123
df = DataFrame({'A': range(5), 'B': range(0, 10, 2)})
@@ -3174,6 +3176,28 @@ def test_rolling_apply(self, raw):
31743176
lambda x: x.rolling(4).apply(lambda y: y.sum(), raw=raw))
31753177
tm.assert_frame_equal(result, expected)
31763178

3179+
def test_rolling_apply_mutability(self):
3180+
# GH 14013
3181+
df = pd.DataFrame({'A': ['foo'] * 3 + ['bar'] * 3, 'B': [1] * 6})
3182+
g = df.groupby('A')
3183+
3184+
mi = pd.MultiIndex.from_tuples([('bar', 3), ('bar', 4), ('bar', 5),
3185+
('foo', 0), ('foo', 1), ('foo', 2)])
3186+
3187+
mi.names = ['A', None]
3188+
# Grouped column should not be a part of the output
3189+
expected = pd.DataFrame([np.nan, 2., 2.] * 2, columns=['B'], index=mi)
3190+
3191+
result = g.rolling(window=2).sum()
3192+
tm.assert_frame_equal(result, expected)
3193+
3194+
# Call an arbitrary function on the groupby
3195+
g.sum()
3196+
3197+
# Make sure nothing has been mutated
3198+
result = g.rolling(window=2).sum()
3199+
tm.assert_frame_equal(result, expected)
3200+
31773201
def test_expanding(self):
31783202
g = self.frame.groupby('A')
31793203
r = g.expanding()

0 commit comments

Comments
 (0)