Skip to content

BUG: Fix issue #61841 - .rolling().mean() returns NaNs on reassignment #61850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

abujabarmubarak
Copy link

This pull request fixes issue #61841, where reassigning a .rolling().mean() result unexpectedly returns a Series of all NaNs, even after copying the DataFrame.


🐛 Bug Reproduction Example

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()  # ❌ Returns NaNs

🔧 What Was Fixed

  • Modified logic in Window._apply():

    • Previously, result slicing ([:: self.step]) broke shape/index alignment.
    • Now it checks for self.step and slices only after full shape result is returned.
# ✅ Fixed
result = self._apply_columnwise(...)
if self.step is not None and self.step > 1:
    if isinstance(result, Series):
        result = result.iloc[:: self.step]
    elif isinstance(result, DataFrame):
        result = result.iloc[:: self.step, :]
return result
  • Moved Series and DataFrame imports to the top level of rolling.py to fix pre-commit check failures related to inconsistent namespace usage.

🧪 How Verified

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())  # ✅ Correct output

✅ Status

  • Bug fixed
  • Code passes all CI and pre-commit checks
  • Imports are consistently handled

Thanks for reviewing this PR!

@abujabarmubarak abujabarmubarak deleted the fix/rolling-mean-imports branch July 14, 2025 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant