diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 728288b686b05..c9267a756bef3 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -780,6 +780,7 @@ Groupby/resample/rolling - :class:`core.window.ewm.ExponentialMovingWindow` now raises a ``NotImplementedError`` when specifying ``times`` with ``adjust=False`` due to an incorrect calculation (:issue:`40098`) - Bug in :meth:`Series.asfreq` and :meth:`DataFrame.asfreq` dropping rows when the index is not sorted (:issue:`39805`) - Bug in aggregation functions for :class:`DataFrame` not respecting ``numeric_only`` argument when ``level`` keyword was given (:issue:`40660`) +- Bug in :class:`core.window.RollingGroupby` where ``as_index=False`` argument in ``groupby`` was ignored (:issue:`39433`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index de5a65108a5cc..d10001c153bd2 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1920,6 +1920,7 @@ def rolling(self, *args, **kwargs): self._selected_obj, *args, _grouper=self.grouper, + _as_index=self.as_index, **kwargs, ) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index bdb7078334163..33b1ceee6e529 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -544,11 +544,13 @@ def __init__( obj: FrameOrSeries, *args, _grouper=None, + _as_index=True, **kwargs, ): if _grouper is None: raise ValueError("Must pass a Grouper object.") self._grouper = _grouper + self._as_index = _as_index # GH 32262: It's convention to keep the grouping column in # groupby., but unexpected to users in # groupby.rolling. @@ -610,6 +612,8 @@ def _apply( ) result.index = result_index + if not self._as_index: + result = result.reset_index(level=list(range(len(groupby_keys)))) return result def _apply_pairwise( diff --git a/pandas/tests/window/test_groupby.py b/pandas/tests/window/test_groupby.py index dd988a4abd9e1..51a6288598c32 100644 --- a/pandas/tests/window/test_groupby.py +++ b/pandas/tests/window/test_groupby.py @@ -732,6 +732,49 @@ def test_groupby_level(self): ) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize( + "by, expected_data", + [ + [["id"], {"num": [100.0, 150.0, 150.0, 200.0]}], + [ + ["id", "index"], + { + "date": [ + Timestamp("2018-01-01"), + Timestamp("2018-01-02"), + Timestamp("2018-01-01"), + Timestamp("2018-01-02"), + ], + "num": [100.0, 200.0, 150.0, 250.0], + }, + ], + ], + ) + def test_as_index_false(self, by, expected_data): + # GH 39433 + data = [ + ["A", "2018-01-01", 100.0], + ["A", "2018-01-02", 200.0], + ["B", "2018-01-01", 150.0], + ["B", "2018-01-02", 250.0], + ] + df = DataFrame(data, columns=["id", "date", "num"]) + df["date"] = to_datetime(df["date"]) + df = df.set_index(["date"]) + + gp_by = [getattr(df, attr) for attr in by] + result = ( + df.groupby(gp_by, as_index=False).rolling(window=2, min_periods=1).mean() + ) + + expected = {"id": ["A", "A", "B", "B"]} + expected.update(expected_data) + expected = DataFrame( + expected, + index=df.index, + ) + tm.assert_frame_equal(result, expected) + class TestExpanding: def setup_method(self):