Skip to content

Commit 343e4ba

Browse files
mroeschkeJulianWgs
authored andcommitted
BUG: RollingGroupby ignored as_index=False (pandas-dev#40789)
1 parent 7fdba20 commit 343e4ba

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ Groupby/resample/rolling
780780
- :class:`core.window.ewm.ExponentialMovingWindow` now raises a ``NotImplementedError`` when specifying ``times`` with ``adjust=False`` due to an incorrect calculation (:issue:`40098`)
781781
- Bug in :meth:`Series.asfreq` and :meth:`DataFrame.asfreq` dropping rows when the index is not sorted (:issue:`39805`)
782782
- Bug in aggregation functions for :class:`DataFrame` not respecting ``numeric_only`` argument when ``level`` keyword was given (:issue:`40660`)
783+
- Bug in :class:`core.window.RollingGroupby` where ``as_index=False`` argument in ``groupby`` was ignored (:issue:`39433`)
783784

784785
Reshaping
785786
^^^^^^^^^

pandas/core/groupby/groupby.py

+1
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,7 @@ def rolling(self, *args, **kwargs):
19251925
self._selected_obj,
19261926
*args,
19271927
_grouper=self.grouper,
1928+
_as_index=self.as_index,
19281929
**kwargs,
19291930
)
19301931

pandas/core/window/rolling.py

+4
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,13 @@ def __init__(
544544
obj: FrameOrSeries,
545545
*args,
546546
_grouper=None,
547+
_as_index=True,
547548
**kwargs,
548549
):
549550
if _grouper is None:
550551
raise ValueError("Must pass a Grouper object.")
551552
self._grouper = _grouper
553+
self._as_index = _as_index
552554
# GH 32262: It's convention to keep the grouping column in
553555
# groupby.<agg_func>, but unexpected to users in
554556
# groupby.rolling.<agg_func>
@@ -610,6 +612,8 @@ def _apply(
610612
)
611613

612614
result.index = result_index
615+
if not self._as_index:
616+
result = result.reset_index(level=list(range(len(groupby_keys))))
613617
return result
614618

615619
def _apply_pairwise(

pandas/tests/window/test_groupby.py

+43
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,49 @@ def test_groupby_level(self):
732732
)
733733
tm.assert_series_equal(result, expected)
734734

735+
@pytest.mark.parametrize(
736+
"by, expected_data",
737+
[
738+
[["id"], {"num": [100.0, 150.0, 150.0, 200.0]}],
739+
[
740+
["id", "index"],
741+
{
742+
"date": [
743+
Timestamp("2018-01-01"),
744+
Timestamp("2018-01-02"),
745+
Timestamp("2018-01-01"),
746+
Timestamp("2018-01-02"),
747+
],
748+
"num": [100.0, 200.0, 150.0, 250.0],
749+
},
750+
],
751+
],
752+
)
753+
def test_as_index_false(self, by, expected_data):
754+
# GH 39433
755+
data = [
756+
["A", "2018-01-01", 100.0],
757+
["A", "2018-01-02", 200.0],
758+
["B", "2018-01-01", 150.0],
759+
["B", "2018-01-02", 250.0],
760+
]
761+
df = DataFrame(data, columns=["id", "date", "num"])
762+
df["date"] = to_datetime(df["date"])
763+
df = df.set_index(["date"])
764+
765+
gp_by = [getattr(df, attr) for attr in by]
766+
result = (
767+
df.groupby(gp_by, as_index=False).rolling(window=2, min_periods=1).mean()
768+
)
769+
770+
expected = {"id": ["A", "A", "B", "B"]}
771+
expected.update(expected_data)
772+
expected = DataFrame(
773+
expected,
774+
index=df.index,
775+
)
776+
tm.assert_frame_equal(result, expected)
777+
735778

736779
class TestExpanding:
737780
def setup_method(self):

0 commit comments

Comments
 (0)