Skip to content

BUG: groupby.apply() inconsistently indexes its return value with the groupby-cols #44803

@ypsah

Description

@ypsah

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame(dict(
    name=["Alice", "Bob", "Carl", "Dan", "Eve"],
    age=[20, 21, 20, 21, 20],
    height=[165, 175, 189, 182, 157],
    weight=[65, 72, 95, 83, 58],
)).set_index("name")

indexed_by_age = df.groupby(["age"]).apply(lambda group: group.copy())
print(indexed_by_age)

print()
print("=" * 30)
print()

indexed_only_by_name = df.groupby(["age"]).apply(lambda group: group)
print(indexed_only_by_name)

Issue Description

The above snippet produces the following output:

           age  height  weight
age name
20  Alice   20     165      65
    Carl    20     189      95
    Eve     20     157      58
21  Bob     21     175      72
    Dan     21     182      83

==============================

       age  height  weight
name
Alice   20     165      65
Bob     21     175      72
Carl    20     189      95
Dan     21     182      83
Eve     20     157      58

Where we can see that despite the return values of the apply-func being semantically identical (group vs. group.copy()), one ends up being indexed with age and name while the other only gets name.

Expected Behavior

I would have expected the two returned dataframes to be the same. I have a personal preference for the first option, with the age appearing in the index, but I think I would be able to work with both.


I know other inconsistencies have been reported:

But IMHO never as clearly as this. I'm hoping this reproducer helps diagnose the issue more easily.
Interestingly, the difference only creeps up when grouping_keys is set to True. Otherwise, we get the second output twice.

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.9.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.6-arch2-1
Version : #1 SMP PREEMPT Thu, 02 Dec 2021 15:47:09 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.4
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.5.3
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 : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions