BUG: rolling with time window does not always pick edge correctly · Issue #40002 · pandas-dev/pandas (original) (raw)


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

Code Sample, a copy-pastable example

data = pd.Series(range(1, 4), index=pd.date_range(start="2020-01-01", end="2020-01-03", freq="1d")).iloc[::-1] data.rolling("1d", closed="left").sum()

Problem description

I'm expecting the result to be

2020-01-03   NaN
2020-01-02   3.0
2020-01-01   2.0
Freq: -1D, dtype: float64

but instead I get

2020-01-03   NaN
2020-01-02   NaN
2020-01-01   NaN
Freq: -1D, dtype: float64

This problem doesn't occur if window is int instead of offset, or if index is in ascending order.
For example:
data.rolling(1, closed="left").sum() yields

2020-01-03    NaN
2020-01-02    3.0
2020-01-01    2.0
Freq: -1D, dtype: float64

and

data = pd.Series(range(1, 4), index=pd.date_range(start="2020-01-01", end="2020-01-03", freq="1d"))
data.rolling("1d", closed="left").sum()

yields

2020-01-01    NaN
2020-01-02    1.0
2020-01-03    2.0
Freq: D, dtype: float64

Expected Output

2020-01-03   NaN
2020-01-02   3.0
2020-01-01   2.0
Freq: -1D, dtype: float64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 9d598a5
python : 3.7.9.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : None.None

pandas : 1.2.1
numpy : 1.19.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.3.3
setuptools : 52.0.0.post20210125
Cython : 0.29.21
pytest : 6.2.2
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.3
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : 0.15.1
pyxlsb : None
s3fs : 0.4.2
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.16.2
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.51.2