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: Performing groupby with as_index=False when df.columns.name is not None drops the name #58024

Closed
3 tasks done
sfc-gh-joshi opened this issue Mar 26, 2024 · 4 comments · Fixed by #58114
Closed
3 tasks done
Assignees

Comments

@sfc-gh-joshi
Copy link

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
df = pd.DataFrame(
    [[1, 2, 3]],
    columns=pd.Index(["A", "B", "C"], name="alpha")
)
df.groupby(by="A", as_index=False)["B"].sum()

Issue Description

Output of the above example:

>>> df
alpha  A  B  C
0      1  2  3
>>> df.groupby(by="A", as_index=False)["B"].sum()
   A  B
0  1  2

The name field of the columns Index object is dropped after the groupby operation.

>>> df.groupby(by="A", as_index=False)["B"].sum().columns
Index(['A', 'B'], dtype='object')

In pandas 1.5, the name was retained:

>>> pd.__version__
'1.5.3'
>>> df.groupby(by="A", as_index=False)["B"].sum()
alpha  A  B
0      1  2
>>> df.groupby(by="A", as_index=False)["B"].sum().columns
Index(['A', 'B'], dtype='object', name='alpha')

Expected Behavior

It is unclear whether this change was intentional between pandas 1.5/2.0; I could not find any mention of this in the issue tracker or release notes. If this was intentional, it should be mentioned briefly in groupby documentation; if not, then it should be fixed to preserve the name as before.

Installed Versions

INSTALLED VERSIONS

commit : 7fe86b6
python : 3.11.7.final.0
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Wed Feb 21 21:45:49 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 3.0.0.dev0+263.g7fe86b67d3
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.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 : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : 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 : 15.0.0
pyreadstat : None
python-calamine : 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

@sfc-gh-joshi sfc-gh-joshi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 26, 2024
@rhshadrach
Copy link
Member

rhshadrach commented Mar 27, 2024

Thanks for the report! This was changed in #50744 and should have been noted in a whatsnew. As noted in that PR, by dropping name, we get consistency between df["B"].groupby(by=...).sum() and df.groupby(by=...)["B"].sum() (with or without as_index=False). Also, I think Series selection in groupby should act similar to Series selection for a DataFrame, and as such the current behavior appears correct to me.

Note that if you use [["B"]] instead, the name is preserved (same as doing df[["B"]]).

I don't believe there is a section detailing column selection in the groupby User Guide. I am in favor of a small section on this, and the name could be mentioned there.

@rhshadrach rhshadrach added Docs Groupby and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 27, 2024
@shriyakalakata
Copy link
Contributor

take

@shriyakalakata
Copy link
Contributor

Hey @rhshadrach! I wanted to clarify what's expected from this task. I see that there is a 'column selection in GroupBy' in the groupby user guide already. I was thinking I could do the following:

  • Add a note in this section about the name
  • Include an example of grouping by a certain column and using another column for aggregation

@rhshadrach
Copy link
Member

Thanks @shriyakalakata - indeed I missed this section!

  • Add a note in this section about the name

This sounds good.

  • Include an example of grouping by a certain column and using another column for aggregation

I don't think this is necessary - there are already examples of this throughout the groupby docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants