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: ValueError on groupby with categoricals #34951

Closed
2 of 3 tasks
LukasGelbmann opened this issue Jun 23, 2020 · 2 comments · Fixed by #35253
Closed
2 of 3 tasks

BUG: ValueError on groupby with categoricals #34951

LukasGelbmann opened this issue Jun 23, 2020 · 2 comments · Fixed by #35253
Assignees
Labels
Bug Categorical Categorical Data Type Groupby
Milestone

Comments

@LukasGelbmann
Copy link

  • 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.


In specific situations involving categorical columns, a groupby() on two or more columns runs into an error:

Code Sample

import pandas as pd
col = pd.Categorical([0, 1])
df = pd.DataFrame({'A': col, 'B': col, 'C': col})
grouped = df.groupby(['A', 'B']).first()
# ValueError: Shape of passed values is (4, 1), indices imply (2, 1)

Expected Output

Expected output would be something like:

>>> grouped
       C
A B     
0 0    0
  1  NaN
1 0  NaN
  1    1

Instead an exception is thrown, with the error message shown above.

@LukasGelbmann LukasGelbmann added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 23, 2020
@rhshadrach rhshadrach added Categorical Categorical Data Type Groupby labels Jun 23, 2020
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Jun 23, 2020
@rhshadrach
Copy link
Member

Thanks for reporting this - I get the same exception on master. Computing this as a Series via df.groupby(['A', 'B']).C.first() works and gives the correct result.

@rhshadrach rhshadrach removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jun 23, 2020
@LucasG0
Copy link
Contributor

LucasG0 commented Jul 4, 2020

take

smithto1 added a commit to smithto1/pandas that referenced this issue Jul 12, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.1 Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants