BUG: rolling window iterator with on should replace with the window index with the on column · Issue #40373 · 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

df = pd.DataFrame({'A': range(10), 'B': range(10,0,-1)}).reset_index() for group in df.rolling(5, on='index'): print(len(group))

Prints:

Problem description

With a rolling window all groups should be always the same size (5 in the above example) and so the first 4 groups should be skipped. The first groups are an expanding window which then turn into a rolling window.

Additionally, the API is a bit unexpected when you compare to a for idx, group in df.groupby('col') operation. With a groupby each element also provides the index, while rolling it just provides the grouped data and removes the grouping index column (i.e. the column index does not exist in group).

Expected Output

Even better it would also include the index so that:

for idx, group in df.rolling(5, on='index'):
    print(len(group))

Would work

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-1100-azure
Version : #111~16.04.1-Ubuntu SMP Thu Nov 19 06:49:21 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.1.post20201107
Cython : 0.29.17
pytest : 6.1.1
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.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.1
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 3.0.0
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : 1.3.20
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.51.2