Commit 8479b2e
Abu Jabar Mubarak
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
1 file changed
+2
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1230 | 1230 | | |
1231 | 1231 | | |
1232 | 1232 | | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
| 1233 | + | |
| 1234 | + | |
1236 | 1235 | | |
1237 | 1236 | | |
1238 | 1237 | | |
| |||
0 commit comments