Skip to content

Consistent Return Structure for Rolling Apply #20984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ Groupby/Resample/Rolling
- Bug in :func:`DataFrame.groupby` where transformations using ``np.all`` and ``np.any`` were raising a ``ValueError`` (:issue:`20653`)
- Bug in :func:`DataFrame.resample` where ``ffill``, ``bfill``, ``pad``, ``backfill``, ``fillna``, ``interpolate``, and ``asfreq`` were ignoring ``loffset``. (:issue:`20744`)
- 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`)
- 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`)

Sparse
^^^^^^
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,7 @@ def _apply(self, func, name=None, window=None, center=None,
index, indexi = self._get_index(index=index)
results = []
for b in blocks:
try:
values = self._prep_values(b.values)
except TypeError:
results.append(b.values.copy())
continue
values = self._prep_values(b.values)

if values.size == 0:
results.append(values.copy())
Expand Down
32 changes: 28 additions & 4 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,19 @@ def test_attribute_access(self):
def tests_skip_nuisance(self):

df = DataFrame({'A': range(5), 'B': range(5, 10), 'C': 'foo'})

r = df.rolling(window=3)
result = r[['A', 'B']].sum()
expected = DataFrame({'A': [np.nan, np.nan, 3, 6, 9],
'B': [np.nan, np.nan, 18, 21, 24]},
columns=list('AB'))
tm.assert_frame_equal(result, expected)

expected = concat([r[['A', 'B']].sum(), df[['C']]], axis=1)
result = r.sum()
tm.assert_frame_equal(result, expected, check_like=True)
def test_skip_sum_object_raises(self):
df = DataFrame({'A': range(5), 'B': range(5, 10), 'C': 'foo'})
r = df.rolling(window=3)

with tm.assert_raises_regex(TypeError, 'cannot handle this type'):
r.sum()

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

def test_rolling_apply_mutability(self):
# GH 14013
df = pd.DataFrame({'A': ['foo'] * 3 + ['bar'] * 3, 'B': [1] * 6})
g = df.groupby('A')

mi = pd.MultiIndex.from_tuples([('bar', 3), ('bar', 4), ('bar', 5),
('foo', 0), ('foo', 1), ('foo', 2)])

mi.names = ['A', None]
# Grouped column should not be a part of the output
expected = pd.DataFrame([np.nan, 2., 2.] * 2, columns=['B'], index=mi)

result = g.rolling(window=2).sum()
tm.assert_frame_equal(result, expected)

# Call an arbitrary function on the groupby
g.sum()

# Make sure nothing has been mutated
result = g.rolling(window=2).sum()
tm.assert_frame_equal(result, expected)

def test_expanding(self):
g = self.frame.groupby('A')
r = g.expanding()
Expand Down