BUG: Fix .rolling().mean() reassignment returning NaNs (pandas-dev#61841) #61851
+13
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request resolves a bug highlighted in issue [#61841](#61841), where reassigning the result of
.rolling().mean()
to the same column in a DataFrame results in all-NaN values after the first assignment.🔜 Root Cause:
The root cause was improper alignment when using the
step
parameter within theWindow._apply()
function. The rolling results were sliced usingself.step
before being fully aligned with the original index, which caused mismatches in the returned Series/DataFrame.🔧 Fix Implemented:
Window._apply()
to applyself.step
only after the result is completely constructed and aligned.Series
andDataFrame
imports from inside a type-checking block (if TYPE_CHECKING
) to the top of the file. This eliminates pre-commit CI errors related to inconsistent namespace usage.📄 Verification:
The fix was verified by executing:
This now works as expected, and outputs the correct rolling mean values.
All relevant pre-commit hooks and CI checks pass after the changes.
Thank you for reviewing this fix!