Skip to content

BUG: groupby.describe with as_index=False incorrect #49643

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

Merged
merged 5 commits into from
Nov 18, 2022

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Nov 11, 2022

The describe bug was found while comparing differences between _selected_obj and _obj_with_exclusions (#46944). By removing as_index from the conditions that determine _selected_obj, we fix the describe bug, the apply inconsistency (see below), and bring _selected_obj and _obj_with_exclusions closer so that the latter can eventually be removed.

Currently groupby(...).apply with as_index=True will try to use the group keys, then if that fails with a TypeError will try to redo the computation without the group keys. In main, this is also attempted when as_index=False, however in that case the selection context does not exclude the group keys and so it's really just attempting the same exact computation again. This PR fixes this inconsistency by making as_index=False adopt the as_index=True behavior. In the one test that picked this up, we now agree with the original behavior that was desired in #7002.

@rhshadrach rhshadrach added Bug Groupby Apply Apply, Aggregate, Transform, Map Nuisance Columns Identifying/Dropping nuisance columns in reductions, groupby.add, DataFrame.apply labels Nov 11, 2022
@rhshadrach rhshadrach added this to the 2.0 milestone Nov 11, 2022
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test where multiple keys are passed into groupby as well e.g. df.groupby(["A", "B"], as_index=False).describe()?

@rhshadrach
Copy link
Member Author

rhshadrach commented Nov 17, 2022

Is there a test where multiple keys are passed into groupby as well e.g. df.groupby(["A", "B"], as_index=False).describe()?

There is now 😄. Added for Series and DataFrame. There are tests for multiple groupings, but I think all cases were not in-axis, so made sense to add these too.

@mroeschke mroeschke merged commit 68e2c2a into pandas-dev:main Nov 18, 2022
@mroeschke
Copy link
Member

Nice thanks @rhshadrach (the groupby describe output looks more sane now)

@rhshadrach rhshadrach deleted the groupby_describe branch November 18, 2022 03:03
MarcoGorelli pushed a commit to MarcoGorelli/pandas that referenced this pull request Nov 18, 2022
* BUG: groupby.describe with as_index=False incorrect

* Add test for two groupings

* Simplify logic
mliu08 pushed a commit to mliu08/pandas that referenced this pull request Nov 27, 2022
* BUG: groupby.describe with as_index=False incorrect

* Add test for two groupings

* Simplify logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby Nuisance Columns Identifying/Dropping nuisance columns in reductions, groupby.add, DataFrame.apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: groupby.describe with as_index=False incorrect
2 participants