BUG: groupby().agg() loses column names for an empty dataframe with 'idxmax' as an aggregation function #42332
Closed
2 of 3 tasks
Labels
Apply
Apply, Aggregate, Transform, Map
good first issue
Groupby
Needs Tests
Unit test(s) needed to prevent regressions
Milestone
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.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
Output:
Problem description
The index returned by
DataFrame.groupby().agg()
is inconsistent depending on the aggregation applied to the groupby for an empty dataframe. This makes it difficult to use the output of these functions without adding subsequent if/else blocks to re-build the index names.In the above examples, the provided DataFrame is empty. The behaviour of the returned DataFrames differs by how I handle the groupby:
In Pandas 1.1.5, the third case behaved well, remembering the column names; the behaviour changed in 1.2.0.
Expected Output
The groupby columns and their names are remembered in all cases.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 7c48ff4
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.31.1.el7.x86_64
Version : #1 SMP Tue Jun 15 10:20:52 CDT 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.2.5
numpy : 1.19.4
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.3
setuptools : 41.6.0
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : 3.5.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.1 (dt dec pq3 ext lo64)
jinja2 : 3.0.1
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.06.0
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1
The text was updated successfully, but these errors were encountered: