Skip to content

BUG: Setting closed='neither' for rolling function of Series leads to mean() producing nan values #39038

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

Closed
1 of 2 tasks
Safeturn opened this issue Jan 8, 2021 · 2 comments
Closed
1 of 2 tasks
Labels
Window rolling, ewma, expanding

Comments

@Safeturn
Copy link

Safeturn commented Jan 8, 2021

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import numpy as np
import pandas as pd

series = pd.Series(np.arange(20), index=pd.date_range("2018-01-01", periods=20, freq="M"))
series.rolling(window=5, closed='neither').mean()

Problem description

In the current version of pandas, the call to mean() produces a series of nan-values. For pandas 1.1.5, the output was different providing valid values starting from the fifth entry of the series.

Expected Output

2018-01-31 NaN
2018-02-28 NaN
2018-03-31 NaN
2018-04-30 NaN
2018-05-31 2.0
2018-06-30 3.0
2018-07-31 4.0
2018-08-31 5.0
2018-09-30 6.0
2018-10-31 7.0
2018-11-30 8.0
2018-12-31 9.0
2019-01-31 10.0
2019-02-28 11.0
2019-03-31 12.0
2019-04-30 13.0
2019-05-31 14.0
2019-06-30 15.0
2019-07-31 16.0
2019-08-31 17.0
Freq: M, dtype: float64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.128-microsoft-standard
Version : #1 SMP Tue Jun 23 12:58:10 UTC 2020
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.0
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0
Cython : 0.29.17
pytest : 6.0.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@Safeturn Safeturn added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2021
@phofl
Copy link
Member

phofl commented Jan 8, 2021

Hi, thanks for your report. In 1.1.4 the closed parameter was ignored for fixed windows and it defaulted to right. The behavior you are showing is for closed=right. I thinkt your result is correct, since left and right both are not in the window so nan everywhere with min_periods=window size, the min-periods parameter needs to be 4 I think.

At least this behaves like that for time aware windows in 1.1.5

@mroeschke Please correct me, if I am wrong.

@phofl phofl added Window rolling, ewma, expanding and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2021
@mroeschke
Copy link
Member

@phofl is correct, xref #34315,

In this case when using neither and a fixed window length, you would need to adjust the min_periods parameter as mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

3 participants