Skip to content

Commit a3201c9

Browse files
BUG: Fix .rolling().mean() reassignment issue and clean up inconsistent imports (#61841)
This commit addresses a bug (issue #61841) in the pandas .rolling().mean() method, where reassigning the result of a rolling mean computation on the same column leads to unexpected NaN values. The root cause was incorrect alignment and slicing when using the step parameter inside the Window._apply() method. The fix ensures the result is properly computed and sliced only after the entire output is generated. Additionally, this update cleans up inconsistent import usage by moving Series and DataFrame imports to the top-level, which resolves pre-commit CI errors. After this fix, repeated assignments using .rolling().mean() behave as expected, and all pre-commit checks pass successfully.
1 parent a2315af commit a3201c9

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pandas/core/window/rolling.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
)
4949
from pandas.core.dtypes.missing import notna
5050

51+
from pandas import (
52+
DataFrame,
53+
Series,
54+
)
5155
from pandas.core._numba import executor
5256
from pandas.core.algorithms import factorize
5357
from pandas.core.apply import (
@@ -119,10 +123,6 @@
119123
npt,
120124
)
121125

122-
from pandas import (
123-
DataFrame,
124-
Series,
125-
)
126126
from pandas.core.generic import NDFrame
127127
from pandas.core.groupby.ops import BaseGrouper
128128

@@ -1230,9 +1230,13 @@ def calc(x):
12301230

12311231
return result
12321232

1233-
return self._apply_columnwise(homogeneous_func, name, numeric_only)[
1234-
:: self.step
1235-
]
1233+
result = self._apply_columnwise(homogeneous_func, name, numeric_only)
1234+
if self.step is not None and self.step > 1:
1235+
if isinstance(result, Series):
1236+
result = result.iloc[:: self.step]
1237+
elif isinstance(result, DataFrame):
1238+
result = result.iloc[:: self.step, :]
1239+
return result
12361240

12371241
@doc(
12381242
_shared_docs["aggregate"],

0 commit comments

Comments
 (0)