Skip to content

BUG: rolling.groupby with on and __getitem__ doesn't mutate underlying object #43374

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
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/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ Groupby/resample/rolling
- Bug in :meth:`pandas.DataFrame.rolling` operation along rows (``axis=1``) incorrectly omits columns containing ``float16`` and ``float32`` (:issue:`41779`)
- Bug in :meth:`Resampler.aggregate` did not allow the use of Named Aggregation (:issue:`32803`)
- Bug in :meth:`Series.rolling` when the :class:`Series` ``dtype`` was ``Int64`` (:issue:`43016`)
- Bug in :meth:`DataFrame.groupby.rolling` when specifying ``on`` and calling ``__getitem__`` would subsequently return incorrect results (:issue:`43355`)

Reshaping
^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,8 @@ def _gotitem(self, key, ndim, subset=None):
# here so our index is carried through to the selected obj
# when we do the splitting for the groupby
if self.on is not None:
self.obj = self.obj.set_index(self._on)
# GH 43355
subset = self.obj.set_index(self._on)
return super()._gotitem(key, ndim, subset=subset)

def _validate_monotonic(self):
Expand Down
39 changes: 39 additions & 0 deletions pandas/tests/window/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,3 +1019,42 @@ def test_times_array(self, times_frame):
result = gb.ewm(halflife=halflife, times="C").mean()
expected = gb.ewm(halflife=halflife, times=times_frame["C"].values).mean()
tm.assert_frame_equal(result, expected)

def test_dont_mutate_obj_after_slicing(self):
# GH 43355
df = DataFrame(
{
"id": ["a", "a", "b", "b", "b"],
"timestamp": date_range("2021-9-1", periods=5, freq="H"),
"y": range(5),
}
)
grp = df.groupby("id").rolling("1H", on="timestamp")
result = grp.count()
expected_df = DataFrame(
{
"timestamp": date_range("2021-9-1", periods=5, freq="H"),
"y": [1.0] * 5,
},
index=MultiIndex.from_arrays(
[["a", "a", "b", "b", "b"], list(range(5))], names=["id", None]
),
)
tm.assert_frame_equal(result, expected_df)

result = grp["y"].count()
expected_series = Series(
[1.0] * 5,
index=MultiIndex.from_arrays(
[
["a", "a", "b", "b", "b"],
date_range("2021-9-1", periods=5, freq="H"),
],
names=["id", "timestamp"],
),
name="y",
)
tm.assert_series_equal(result, expected_series)
# This is the key test
result = grp.count()
tm.assert_frame_equal(result, expected_df)