Skip to content

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

Closed
@MrToustous

Description

@MrToustous
  • [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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions