Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Dropped Index Name on Aggregated Index Column with MultiIndexed DataFrame #60603

Open
3 tasks done
RahimD opened this issue Dec 24, 2024 · 1 comment
Open
3 tasks done
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Groupby

Comments

@RahimD
Copy link

RahimD commented Dec 24, 2024

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

df = pandas.DataFrame({"A" : [1,2,3,1,2,3], "B": [4,5,6,4,5,6], "C" : [7,8,9,7,8,9]})
df = df.set_index(['A', 'B'])
df.groupby(lambda x : x[0]).aggregate('sum')

Issue Description

When using a lambda to group a DataFrame by, even though the lambda is using a pre-existing index from the DataFrame, the resulting index in the aggregation does not take its name. Comparatively, using df.groupby('A') in the above example would yield an index with the index name 'A'. As best as I can tell, this happens because of this, which requests name[0] from the BaseGrouper object, which ultimately seems to get its names from the underlying Grouping object. However, when getting that name, since self.grouping_vector is a MultiIndex, the elif isinstance(self.grouping_vector, Index): branch is taken, which leads to a request for the .name attribute of a MultiIndex, which returns None. This names of index columns are dropped even if a list of lambdas is provided in order to produce multiple levels of a MultiIndex in the aggregation output, i.e. even if len(self.groupings) > 1 in the BaseGrouper object

Apologies in advance if this is intended behaviour.

Expected Behavior

df = pandas.DataFrame({"A" : [1,2,3,1,2,3], "B": [4,5,6,4,5,6], "C" : [7,8,9,7,8,9]})
df = df.set_index(['A', 'B'])
df.groupby('A')

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.1
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18363
machine : AMD64
processor : AMD64 Family 21 Model 96 Stepping 1, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Canada.1252

pandas : 2.2.3
numpy : 2.2.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@RahimD RahimD added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 24, 2024
@rhshadrach
Copy link
Member

Thanks for the report!

When using a lambda to group a DataFrame by, even though the lambda is using a pre-existing index from the DataFrame

pandas has no knowledge of how the lambda acts. So it seems the current behavior is appropriate.

@rhshadrach rhshadrach added Groupby Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Groupby
Projects
None yet
Development

No branches or pull requests

2 participants