Skip to content

Conversation

abujabarmubarak
Copy link

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 the Window._apply() function. The rolling results were sliced using self.step before being fully aligned with the original index, which caused mismatches in the returned Series/DataFrame.

🔧 Fix Implemented:

  • Adjusted the logic in Window._apply() to apply self.step only after the result is completely constructed and aligned.
  • Moved Series and DataFrame 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:

import pandas as pd
import numpy as np

df = pd.DataFrame({"Close": np.arange(1, 31)})
df = df.copy()
df["SMA20"] = df["Close"].rolling(20).mean()
df["SMA20"] = df["Close"].rolling(20).mean()
print(df.tail())

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!

@simonjayhawkins simonjayhawkins added Bug Window rolling, ewma, expanding labels Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Window rolling, ewma, expanding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants