Skip to content
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

BUG: rolling window with center=True, min_periods=1 is not symmetric at edges #59252

Closed
2 of 3 tasks
jack-walp opened this issue Jul 16, 2024 · 7 comments
Closed
2 of 3 tasks
Assignees
Labels
Bug Window rolling, ewma, expanding

Comments

@jack-walp
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
pd.Series(np.arange(100)).rolling(21, center=True, min_periods=1).mean().plot()

Issue Description

The np.arange gives a simple linear trend that should not be affected by the rolling mean filter. However at the edges the mean filter pulls values more towards the centre than expected, causing kinks in the curve. It looks like at the edge of the data Null values creep into the window and these are ignored by the mean filter. Because Null values only creep into one side of the window the effective centre value gets offset.

Expected Behavior

I would expect both sides of the window to be shrunk so that the point under examination is the centre of the live data.

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.12.2.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.36.2.el7.x86_64
Version : #1 SMP Wed Jul 21 11:57:15 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.1.1
pip : 24.0
Cython : 3.0.10
pytest : None
hypothesis : None
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.22.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : 2.4.1
pyqt5 : None

@jack-walp jack-walp added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 16, 2024
@jack-walp
Copy link
Author

image

@sgysherry
Copy link

take

@sgysherry
Copy link

Hi , could you try with pd.Series(np.arange(100)).rolling(21, center=True).mean().plot()?
The reason why it is creating a kink is that you set min_periods=1, which make the first value of rolling().mean() to be (0+...+10)/11=5.

issue_59252

If this is not the case you are talking about, could you please give me some more explanation on what is the expected output?

@jack-walp
Copy link
Author

jack-walp commented Aug 1, 2024

If you don't have min_periods=1 you lose the data at the edges. I want a window that shrinsk at the edges but remains symmetric. The reason you get the kinks at the edges is because the window is no longer centred (with live data), it is an artefact of having the window become assymetric.

@rhshadrach
Copy link
Member

@jack-walp - for the first data point are you wanting the window size to be 1, the 2nd the window size to be 3, etc, until 21 is hit and then the window size is 21?

@rhshadrach rhshadrach added Needs Info Clarification about behavior needed to assess issue Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 26, 2024
@jack-walp
Copy link
Author

@rhshadrach yes, that's correct. The key thing is that the window is symmetric about the evaluation point.

@rhshadrach
Copy link
Member

Expanding the window size is not how the rolling window in pandas is supposed to operate by default, so is not a bug. This can be accomplished by Custom window rolling. Here is an example for this case (I've only checked it on odd window sizes):

window_size = 21
ser = pd.Series(np.arange(100))
from pandas.api.indexers import BaseIndexer

class CustomIndexer(BaseIndexer):
    def get_window_bounds(self, num_values, min_periods, center, closed, step):
        start = np.empty(num_values, dtype=np.int64)
        end = np.empty(num_values, dtype=np.int64)
        for i in range(num_values):
            to_end = min(i, num_values - i - 1, (self.window_size - 1) // 2)
            if i > to_end:
                start[i] = i - to_end
            else:
                start[i] = 0
            if num_values - i > to_end:
                end[i] = i + to_end + 1
            else:
                end[i] = num_values
        return start, end

Closing.

@rhshadrach rhshadrach removed the Needs Info Clarification about behavior needed to assess issue label Nov 5, 2024
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

No branches or pull requests

3 participants