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: Inconsistent handling of empty windows for rolling mean with min_periods=0 #41053

Closed
1 task
MrToustous opened this issue Apr 20, 2021 · 4 comments · Fixed by #41106
Closed
1 task

BUG: Inconsistent handling of empty windows for rolling mean with min_periods=0 #41053

MrToustous opened this issue Apr 20, 2021 · 4 comments · Fixed by #41106
Labels
Bug Window rolling, ewma, expanding
Milestone

Comments

@MrToustous
Copy link

  • [x ] 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

tmp = pd.DataFrame(
    [
        0.03, 
        0.03, 
        0.001, 
        np.NaN, 
        0.002, 
        0.008, 
        np.NaN, 
        np.NaN, 
        np.NaN, 
        np.NaN, 
        np.NaN, 
        np.NaN, 
        0.005, 
        0.2
    ]
)
tmp.iloc[1:].rolling(5, min_periods=0).mean() # Returns 0 at indexes 10 and 11
tmp.iloc[2:].rolling(5, min_periods=0).mean() # Returns np.NaN at indexes 10 and 11

Problem description

Pandas rolling mean computation inconsistently treats windows with NaN values only, depending on how far they are in the DataFrame. Here we can see how the values for the windows indexed as 10 and 11 are set to 0 or NaN depending on whether we start the computation starting from the 2nd observation or the 3rd.
Other aggregation functions (like sum) don't seem to suffer from this
This only happens when min_periods is set to 0
This doesn't happen in pandas version 1.0.3
This doesn't happen if non NaN values are set to 1 instead of floating values
This could be linked to another reported issue that I haven't identified after a quick search

Expected Output

The expected output would be for the mean computation to return np.NaN for the indexes 10 and 11 in both cases, consistent with pandas documentation and other aggregation functions behavior

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.7.10.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-142-generic
Version : #146-Ubuntu SMP Tue Apr 13 01:11:19 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.3
numpy : 1.19.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : 0.29.22
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: 0.9.0
bs4 : None
bottleneck : None
fsspec : 0.8.7
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.17.0
xlrd : None
xlwt : None
numba : 0.53.1

@MrToustous MrToustous added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 20, 2021
@phofl
Copy link
Member

phofl commented Apr 20, 2021

Actually I am getting not NaN for sum.

tmp = pd.DataFrame(
    [
        0.03,
        0.03,
        0.001,
        np.NaN,
        0.002,
        0.008,
        np.NaN,
        np.NaN,
        np.NaN,
        np.NaN,
        np.NaN,
        np.NaN,
        0.005,
        0.2
    ]
)
print(tmp.iloc[1:].rolling(5, min_periods=0).sum())

returns

               0
1   3.000000e-02
2   3.100000e-02
3   3.100000e-02
4   3.300000e-02
5   4.100000e-02
6   1.100000e-02
7   1.000000e-02
8   1.000000e-02
9   8.000000e-03
10 -3.469447e-18
11 -3.469447e-18
12  5.000000e-03
13  2.050000e-01

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

phofl commented Apr 20, 2021

@mroeschke Can you help here? Is NaN correct with min_periods=0?

@mroeschke
Copy link
Member

Yes, there appears to be a bug here somewhere.

min_periods=0 essentially means calculate the result with the given window. In this example, there is a point where there's a window of all NaNs, so the expected results in this case should be:

In [31]: s = pd.Series([np.nan] * 5)

In [32]: s
Out[32]:
0   NaN
1   NaN
2   NaN
3   NaN
4   NaN
dtype: float64

In [33]: s.sum()
Out[33]: 0.0

In [34]: s.mean()
Out[34]: nan

@MrToustous
Copy link
Author

MrToustous commented Apr 21, 2021

Actually I am getting not NaN for sum.

tmp = pd.DataFrame(
    [
        0.03,
        0.03,
        0.001,
        np.NaN,
        0.002,
        0.008,
        np.NaN,
        np.NaN,
        np.NaN,
        np.NaN,
        np.NaN,
        np.NaN,
        0.005,
        0.2
    ]
)
print(tmp.iloc[1:].rolling(5, min_periods=0).sum())

returns

               0
1   3.000000e-02
2   3.100000e-02
3   3.100000e-02
4   3.300000e-02
5   4.100000e-02
6   1.100000e-02
7   1.000000e-02
8   1.000000e-02
9   8.000000e-03
10 -3.469447e-18
11 -3.469447e-18
12  5.000000e-03
13  2.050000e-01

Indeed. However this seems to be by design and the behavior looks consistent. What's weird is that the reported bug seems to appear depending on the size of the DataFrame used in the rolling mean methods

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 a pull request may close this issue.

3 participants