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

REF: groupby Series selection with as_index=False #50744

Merged
merged 6 commits into from
Jan 18, 2023

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Jan 14, 2023

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Currently .groupby(..., as_index=False)['column'] gives back a DataFrameGroupBy This is because SeriesGroupBy doesn't implement as_index=False. This gives rise to having a DataFrameGroupBy object with self.obj being a Series. It also might be a bit surprising to users that this gives them back a DataFrameGroupBy (though this is probably rarely noticed).

Eliminating this case simplifies the states groupby code must consider. It also is a step toward #46944.

There should be no changes in behavior from a user perspective except getting back a SeriesGroupBy and the one noted in the comments below.

cc @jbrockmendel

@rhshadrach rhshadrach added Refactor Internal refactoring of code Groupby labels Jan 14, 2023
Comment on lines 655 to 656
# GH#?? - Columns after selection shouldn't retain names
expected.columns.names = [None]
Copy link
Member Author

@rhshadrach rhshadrach Jan 14, 2023

Choose a reason for hiding this comment

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

This is a change in behavior; currently .groupby(..., as_index=False)["B"] retains the columns' name. We could still preserve the index name here, but it'd be a bit of a hack to add on. By not preserving the name, we make df["A"].groupby(..., as_index=False) behave the same as df.groupby(..., as_index=False)["A"], which I think is desirable.

But would appreciate others thoughts here.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm I suppose in theory it would be nice if both operations kept the column name since the result always contains portion of the old column object?

But in this case I suppose it's minor and there's an argument that a groupby result produces a "new column"

results.append(new_res)
is_groupby = isinstance(obj, (DataFrameGroupBy, SeriesGroupBy))
context_manager: ContextManager
if is_groupby:
Copy link
Member

Choose a reason for hiding this comment

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

Kinda unfortunate that groupby knowledge is leaking in pandas/core/apply.py, guessing there's no way to avoid this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. I don't have thoughts on how to improve this just yet, but after removing obj_with_exclusions (we're almost there I think), I plan to try to cleanup the paths through groupby.agg, etc. Without a refactor, we need this because other objects don't have as_index.

@mroeschke mroeschke added this to the 2.0 milestone Jan 18, 2023
@mroeschke mroeschke merged commit 5d11658 into pandas-dev:main Jan 18, 2023
@mroeschke
Copy link
Member

Thanks @rhshadrach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants