Skip to content

EHN: Implement closed=left|right|neither in DataFrame.rolling for fixed windows #34315

Closed
@victorbrjorge

Description

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

  • 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 pandas as pd

data = [0, 1, 1, 0, 0, 1, 0, 1]
df = pd.DataFrame({'binary_col': data}, index=pd.date_range(start='2020-01-01', freq="min", periods=len(data)))

rolling = df.rolling(window=len(df), closed='left', min_periods=1)
rolling.mean()

Problem description

The rolling window ignores the closed='left' parameter. Since it was supposed to be closed only on the left side, the current right side of the window should not be used on the calculations. But it turns out it is.

Original dataframe

index binary_col
2020-01-01 00:00:00 0
2020-01-01 00:01:00 1
2020-01-01 00:02:00 1
2020-01-01 00:03:00 0
2020-01-01 00:04:00 0
2020-01-01 00:05:00 1
2020-01-01 00:06:00 0
2020-01-01 00:07:00 1

Actual output

index binary_col
2020-01-01 00:00:00 0.000000
2020-01-01 00:01:00 0.500000
2020-01-01 00:02:00 0.666667
2020-01-01 00:03:00 0.500000
2020-01-01 00:04:00 0.400000
2020-01-01 00:05:00 0.500000
2020-01-01 00:06:00 0.428571
2020-01-01 00:07:00 0.500000

Expected Output

index binary_col
2020-01-01 00:00:00 NaN
2020-01-01 00:01:00 0.000000
2020-01-01 00:02:00 0.500000
2020-01-01 00:03:00 0.666667
2020-01-01 00:04:00 0.500000
2020-01-01 00:05:00 0.400000
2020-01-01 00:06:00 0.500000
2020-01-01 00:07:00 0.428571

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-70-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : pt_BR.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.2.0.post20200511
Cython : None
pytest : 5.4.2
hypothesis : None
sphinx : 3.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementError ReportingIncorrect or improved errors from pandasWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions