Skip to content

BUG: df.rolling.{std, skew, kurt} gives unexpected value #61416

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

Open
3 tasks done
Jie-Lei opened this issue May 9, 2025 · 7 comments
Open
3 tasks done

BUG: df.rolling.{std, skew, kurt} gives unexpected value #61416

Jie-Lei opened this issue May 9, 2025 · 7 comments
Assignees
Labels
Bug Window rolling, ewma, expanding

Comments

@Jie-Lei
Copy link

Jie-Lei commented May 9, 2025

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
df = pd.DataFrame(index=range(100))
df = df.assign(val = df.index)
df = df/1e3

df.loc[0,"val"] = 1e6
df.loc[5,"val"] = -1e6

res1 = df.rolling(20,min_periods=1).kurt()
res2 = df.iloc[1:].rolling(20,min_periods=1).kurt()

>>>res1.tail(5)
           val
95  722.329422
96  730.791755
97  739.254087
98  747.716420
99  756.178752
>>>res2.tail(5)
    val
95 -1.2
96 -1.2
97 -1.2
98 -1.2
99 -1.2

Issue Description

In one of my experiments, the results of my rolling calculation of high-order moments differed. When I excluded the first data or retained the first data, the results of the rolling calculation varied greatly. I used this case to attempt to reproduce this result. The operators I tested, Including df.rolling.std, df.rolling.skew, df.rolling.kurt. I don't know what the reason is. I think for the df.rolling operator, this should be a bug

Expected Behavior

The result of the rolling calculation, regardless of what the first one is, should the last few pieces of data not be affected by the initial data

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 106 Stepping 6, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : Chinese (Simplified)_China.936

pandas : 2.2.3
numpy : 2.2.5
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.1.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None
None

@Jie-Lei Jie-Lei added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 9, 2025
@arthurlw
Copy link
Member

arthurlw commented May 9, 2025

Hey OP, thanks for raising this! Rolling operations include not just the current row, but also previous rows within the window. This means including or excluding the first row can impact the entire calculation, even for later rows. This is expected behavior, not a bug.

Let me know if this makes sense or if you’re seeing something different.

@arthurlw arthurlw added Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 9, 2025
@Jie-Lei
Copy link
Author

Jie-Lei commented May 10, 2025

@arthurlw Thank you for your reply! Regarding the function of df.rolling, I believe it builds a sliding window on the data and applies a calculation function. Each calculation only uses the data within the window. If following this logic, in the case I provided, when calculating the kurtosis, the following results will be my expectation:

res1 = df.rolling(20,min_periods=1).kurt()
res2 = df.iloc[1:].rolling(20,min_periods=1).kurt()

pd.testing.assert_frame_equal(res1.loc[21:], res2.loc[21:])

However, from the case I presented, it can be seen that the results are not like this. In my case, I constructed a special dataframe, which has a maximum value and a minimum value. The maximum value is located at index 0. Whether to include this value in the rolling calculation will lead to different results.

This is my understanding of df.rolling. Finally, once again, thank you for your reply.

@arthurlw
Copy link
Member

Thanks for the explanation! You’re right that whether the first data point (index 0) is included will lead to different results. This is actually expected behavior because df.iloc[1:] explicitly removes the first row, which means all window calculations in res2 will start at index 1 and will exclude the value at index 0. Thus, res1 and res2 will provide different results.

@Jie-Lei
Copy link
Author

Jie-Lei commented May 11, 2025

Thank you for your reply. What surprises me is that the sliding window calculation shouldn't be affected by data outside the window. Then, including or excluding the first piece of data not affect the calculation result at the last index position. Since the data window is 20 and there are 100 data samples, why excluding the first data entry would cause the calculation result at the last index position to be different? This is the point that raises my doubts.

@arthurlw
Copy link
Member

arthurlw commented May 11, 2025

I see now what you mean and thanks for the catch! This definitely shouldn’t happen. It looks like the huge outlier is influencing values outside of its window with .std, .skew, and .kurt. PRs and contributions are welcome.

@arthurlw arthurlw added Window rolling, ewma, expanding and removed Needs Info Clarification about behavior needed to assess issue labels May 11, 2025
@eicchen
Copy link
Contributor

eicchen commented May 11, 2025

take

@auderson
Copy link
Contributor

Rolling algos in pandas uses online methods, see: #60053 (comment)

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

4 participants