Skip to content

Commit 3da3d5a

Browse files
author
Abu Jabar Mubarak
authored
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 3da3d5a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

pandas/core/window/rolling.py

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

51+
52+
from pandas import (
53+
DataFrame,
54+
Series,
55+
)
5156
from pandas.core._numba import executor
5257
from pandas.core.algorithms import factorize
5358
from pandas.core.apply import (
@@ -119,10 +124,7 @@
119124
npt,
120125
)
121126

122-
from pandas import (
123-
DataFrame,
124-
Series,
125-
)
127+
126128
from pandas.core.generic import NDFrame
127129
from pandas.core.groupby.ops import BaseGrouper
128130

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

12311233
return result
12321234

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

12371243
@doc(
12381244
_shared_docs["aggregate"],

0 commit comments

Comments
 (0)