Skip to content

BUG: Attributes of DataFrameGroupBy when subset to a series #35443

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

Closed
rhshadrach opened this issue Jul 29, 2020 · 6 comments · Fixed by #51079
Closed

BUG: Attributes of DataFrameGroupBy when subset to a series #35443

rhshadrach opened this issue Jul 29, 2020 · 6 comments · Fixed by #51079
Labels
API - Consistency Internal Consistency of API/Behavior Bug Groupby

Comments

@rhshadrach
Copy link
Member

rhshadrach commented Jul 29, 2020

xref #9959

When subsetting the columns of a dataframe after a groupby to a series:

df = pd.DataFrame({'a': [1], 'b': [2], 'c': [3]}).set_index('a')
g = df.groupby('a')['b']

the public-facing groupby arguments axis and as_index are not passed on to the resulting SeriesGroupBy.

I think the following two examples should probably behave the same:

g = df.groupby('a', as_index=False)['b']
g2 = df['b'].groupby('a', as_index=False)

but the first produces

   b
0  2

And the 2nd deliberately raises TypeError: as_index=False only valid with DataFrame.

Note that the arguments axis and as_index are only meant for DataFrames, and as such, I think a line such as

g = df.groupby('a', as_index=False)['b']

should raise.

@rhshadrach rhshadrach added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 29, 2020
@rhshadrach rhshadrach added Groupby Needs Discussion Requires discussion from core team before further action API - Consistency Internal Consistency of API/Behavior and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 29, 2020
@rhshadrach
Copy link
Member Author

I updated the OP to only include the two attributes that would cause a potential API change.

@rhshadrach
Copy link
Member Author

rhshadrach commented Aug 31, 2020

After rethinking this, perhaps instead

df['b'].groupby('a', as_index=False)

should not raise, but rather be the same as df.groupby('a', as_index=False)['b'] I don't yet know how difficult this would be.

@mroeschke mroeschke removed the Needs Discussion Requires discussion from core team before further action label Aug 8, 2021
@jbrockmendel
Copy link
Member

@rhshadrach you've done a lot of work in this area since the OP. any chance this has been resolved?

@rhshadrach
Copy link
Member Author

rhshadrach commented Jan 27, 2023

For

df = pd.DataFrame({'a': [1], 'b': [2], 'c': [3]}).set_index('a')
g = df.groupby([0, 1], axis=1)['b']
g2 = df['b'].groupby('a', axis=1)

both lines g and g2 raise correctly (I think this has always been the case). For as_index, SeriesGroupBy now fully (I believe) supports it but we raise in Series.groupby if you try to pass as_index=False. I'm in favor of allowing as_index=False here, but that's #36507. Closing in favor of that issue.

@jbrockmendel
Copy link
Member

jbrockmendel commented Jan 30, 2023

Looks like test_subsetting_columns_keeps_attrs has an xfail for SeriesGroupBy with axis=1, not bc the attribute is failing to be inherited but bc the subsetting operation is raising when it tries to do result = expected["b"]

@rhshadrach
Copy link
Member Author

I think this is expected behavior and shouldn't be an xfail. I'll move it to it's own test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants