BUG: DataFrameGroupBy.rolling on an empty DataFrame throws a ValueError · Issue #36197 · pandas-dev/pandas (original) (raw)


Code Sample, a copy-pastable example

import pandas as pd pd.DataFrame({"s1": []}).groupby("s1").rolling(window=1).sum()

Problem description

Since a pandas update, the groupby above started failing in the edge case of an empty DataFrame. It used to simply return an empty DataFrame before, which seems like a more robust behaviour to me.

Expected Output

>>> pd.DataFrame({"s1": []}).groupby("s1").rolling(window=1).sum()
Empty DataFrame
Columns: []
Index: []

Actual Output

>>> pd.DataFrame({"s1": []}).groupby("s1").rolling(window=1).sum()'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "site-packages/pandas/core/window/rolling.py", line 2072, in sum
    return super().sum(*args, **kwargs)
  File "site-packages/pandas/core/window/rolling.py", line 1424, in sum
    window_func, center=self.center, floor=0, name="sum", **kwargs
  File "site-packages/pandas/core/window/rolling.py", line 2194, in _apply
    **kwargs,
  File "site-packages/pandas/core/window/rolling.py", line 528, in _apply
    blocks, obj = self._create_blocks(self._selected_obj)
  File "site-packages/pandas/core/window/rolling.py", line 2230, in _create_blocks
    list(self._groupby.grouper.indices.values())
  File "<__array_function__ internals>", line 6, in concatenate
ValueError: need at least one array to concatenate

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2ca0a2
python : 3.7.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.7.17-200.fc32.x86_64
Version : #1 SMP Fri Aug 21 15:23:46 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.1
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.7.3
pip : 19.3.1
setuptools : 42.0.2
Cython : None
pytest : 5.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.3.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.3.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.0
fastparquet : None
gcsfs : None
matplotlib : 3.0.3
numexpr : None
odfpy : None
openpyxl : 3.0.4
pandas_gbq : None
pyarrow : 0.12.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None