diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index c6991bc016868..32f7447e5ef77 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -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() ` where operations performed against the associated :class:`DataFrameGroupBy` object could impact the inclusion of the grouped item(s) in the result (:issue:`14013`) Sparse ^^^^^^ diff --git a/pandas/core/window.py b/pandas/core/window.py index d7f9f7c85fbbc..5fd054b1930e6 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -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()) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index 304e3d02466a5..93f637a561718 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -105,7 +105,6 @@ 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], @@ -113,9 +112,12 @@ def tests_skip_nuisance(self): 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)}) @@ -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()