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: groupby.agg with as_index=False and duplicate columns in by fails with pd.NamedAgg. #58446

Closed
2 of 3 tasks
sfc-gh-rdurrani opened this issue Apr 26, 2024 · 5 comments · Fixed by #58579
Closed
2 of 3 tasks
Assignees
Labels
Apply Apply, Aggregate, Transform good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions

Comments

@sfc-gh-rdurrani
Copy link

sfc-gh-rdurrani commented Apr 26, 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

import pandas as pd
df = pd.DataFrame(
           {
               "col1": [2, 1, 1, 0, 2, 0],
               "col2": [4, 5, 36, 7, 4, 5],
               "col3": [3.1, 8.0, 12, 10, 4, 1.1],
               "col4": [17, 3, 16, 15, 5, 6],
               "col5": [-1, 3, -1, 3, -2, -1],
           }
)

df.groupby(by=["col1", "col1", "col2"], as_index=False).agg({"col1": [min, max], "col2": "count"}) # works
df.groupby(by=["col1", "col1", "col2"], as_index=False).agg(new_col=pd.NamedAgg('col1', min), new_col1=pd.NamedAgg('col1', max), new_col2=('col2', 'count')) # Fails

Issue Description

df.groupby with duplicate columns in by works with agg when a dictionary is passed in for aggregation function, but fails when pd.NamedAgg is used to provide aggregation functions.

Expected Behavior

Should work, and have a similar output as passing in a dictionary.

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.10.14.final.0
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : 5.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.23.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.3.1
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 16.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

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

rhshadrach commented Apr 26, 2024

Thanks for the report. You checked the box I have confirmed this bug exists on the main branch of pandas. I am seeing the following output on main:

print(df.groupby(by=["col1", "col1", "col2"], as_index=False).agg(new_col=pd.NamedAgg('col1', min), new_col1=pd.NamedAgg('col1', max), new_col2=('col2', 'count')))
#    col1  col2  new_col  new_col1  new_col2
# 0     0     5        0         0         1
# 1     0     7        0         0         1
# 2     1     5        1         1         1
# 3     1    36        1         1         1
# 4     2     4        2         2         2

Can you confirm if you are seeing an error or the above output on main?

@rhshadrach rhshadrach added Needs Info Clarification about behavior needed to assess issue Groupby Apply Apply, Aggregate, Transform and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 26, 2024
@sfc-gh-rdurrani
Copy link
Author

I can't seem to install pandas from source - I keep getting the following error:

ModuleNotFoundError: No module named 'pandas._libs.pandas_parser'

but I did notice that the bug is happening on the latest version of pandas - 2.2.2.

@VISWESWARAN1998
Copy link
Contributor

@sfc-gh-rdurrani Your code runs fine without any errors in main branch for me.

I can't seem to install pandas from source - I keep getting the following error:

ModuleNotFoundError: No module named 'pandas._libs.pandas_parser'

but I did notice that the bug is happening on the latest version of pandas - 2.2.2.

Try creating an isolated environment using mamba (recommended) or pip, install the requirements in requirements-dev.txt and try installing pandas from source. More information here: https://pandas.pydata.org/docs/dev/development/contributing_environment.html

@rhshadrach rhshadrach added Needs Tests Unit test(s) needed to prevent regressions and removed Bug Needs Info Clarification about behavior needed to assess issue labels Apr 30, 2024
@rhshadrach
Copy link
Member

Thanks @sfc-gh-rdurrani - there isn't a requirement to try it out on main (but you're certainly welcome to!). I just wanted to make sure I wasn't making a mistake when I see this is fixed on the main branch.

@jasonmokk
Copy link
Contributor

take

jasonmokk pushed a commit to jasonmokk/pandas that referenced this issue May 5, 2024
mroeschke pushed a commit that referenced this issue May 6, 2024
…amedAgg` and `asIndex=False` (#58579)

* implement test for GH #58446

* Reformat GH issue comment

* Directly inline as_index=False in groupby call

---------

Co-authored-by: Jason Mok <jasonmok@Jasons-MacBook-Air-4.local>
pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this issue May 7, 2024
…amedAgg` and `asIndex=False` (pandas-dev#58579)

* implement test for GH pandas-dev#58446

* Reformat GH issue comment

* Directly inline as_index=False in groupby call

---------

Co-authored-by: Jason Mok <jasonmok@Jasons-MacBook-Air-4.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
4 participants