Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
X = pd.DataFrame({"group": [1, 2, 2, 3, 3, 3], "other": [1, 1, 1, 1, 1, 1]})
X.groupby(["group"])[["group"]].rolling(2, min_periods=1).agg("count")
Issue Description
A DataFrame with no columns is returned.
Empty DataFrame
Columns: []
Index: [(1, 0), (2, 1), (2, 2), (3, 3), (3, 4), (3, 5)]
As an FYI, X.groupby(["group"])["group"].rolling(2, min_periods=1).agg("count")
seems fine
group
1 0 1.0
2 1 1.0
2 2.0
3 3 1.0
4 2.0
5 2.0
Name: group, dtype: float64
as does
X.groupby(["group"])[["other"]].rolling(2, min_periods=1).agg("count")
other
group
1 0 1.0
2 1 1.0
2 2.0
3 3 1.0
4 2.0
5 2.0
as does
X.groupby(["group"])["other"].rolling(2, min_periods=1).agg("count")
group
1 0 1.0
2 1 1.0
2 2.0
3 3 1.0
4 2.0
5 2.0
Name: other, dtype: float64
Also, the problem does not seem to persist if we don't perform a non collapsing aggregation:
X.groupby(["group"])[["group"]].agg("count")
group
group
1 1
2 2
3 3
but does if we perform expanding aggregation:
X.groupby(["group"])[["group"]].expanding(min_periods=1).agg("count")
Empty DataFrame
Columns: []
Index: [(1, 0), (2, 1), (2, 2), (3, 3), (3, 4), (3, 5)]
Expected Behavior
It would output the same as when one does
X.groupby(["group"])[["other"]].rolling(2, min_periods=1).agg("count")
but with "group"
as the column name.
Installed Versions
INSTALLED VERSIONS
------------------
commit : a671b5a8bf5dd13fb19f0e88edc679bc9e15c673
python : 3.10.11.final.0
python-bits : 64
OS : Darwin
OS-release : 22.4.0
Version : Darwin Kernel Version 22.4.0: Mon Mar 6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.1.4
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.19.0
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None