Skip to content

Commit 8479b2e

Browse files
author
Abu Jabar Mubarak
authored
BUG: Fix reassignment issue in rolling().mean() due to slicing misalignment (#61841)
This commit resolves issue #61841 where reassigning the result of .rolling().mean() on a copied DataFrame column results in all NaN values starting in Pandas 2.2.x and persisting into main. ### Problem: The bug occurs when the same .rolling().mean() operation is executed multiple times, e.g.: df["SMA20"] = df["Close"].rolling(20).mean() df["SMA20"] = df["Close"].rolling(20).mean() The first assignment works correctly. The second assignment unexpectedly results in an entire column of NaNs, even though the input data is valid. This occurs because of the line: return self._apply_columnwise(...)[:: self.step] The slicing [:: self.step] alters the shape of the result array during reassignment. When Pandas tries to align the sliced result back to the full DataFrame, it cannot match the shape, resulting in all values being cast to NaN. ### Fix: This patch removes the [:: self.step] slicing from the return line in _apply_columnwise(). Instead, it stores the full result in a variable and returns it directly: result = self._apply_columnwise(...) return result This change ensures: - Consistent shape of output regardless of reassignment. - Correct behavior even when .mean() is run multiple times. - No breakage to existing functionality. ### Notes: - This fix was originally necessary for 2.2.x but still applies to main for consistency and reliability. - Verified with a minimal reproducible example using `yfinance` and also synthetic DataFrame data (e.g., `df = pd.DataFrame({"Close": range(1, 31)})`).
1 parent a2315af commit 8479b2e

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

pandas/core/window/rolling.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,9 +1230,8 @@ 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+
return result
12361235

12371236
@doc(
12381237
_shared_docs["aggregate"],

0 commit comments

Comments
 (0)