Skip to content

DOC: GH5456 Adding workaround info on NA / NaT handling for groupby #47337

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
wants to merge 1 commit into from

Conversation

aamnv
Copy link
Contributor

@aamnv aamnv commented Jun 13, 2022

Took a stab at this by parsing through the initial issue and related topics - let me know if the intent was different, happy to edit.

@@ -1268,6 +1268,11 @@ automatically excluded. In other words, there will never be an "NA group" or
generally discarding the NA group anyway (and supporting it was an
implementation headache).

.. note::
If you need to include NaN or NaT values in your grouping, you can workaround the automatic exclusion by replacing
the NaN / NaT values with a placeholder string. For example, you can use ``df.fillna("default", inplace=True)`` to
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the PR!

  1. Could you demonstrate this by including a .. ipython:: python example (like above)
  2. In the example, could you exclude inplace=True? We are trying to discourage that keyword generally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do on both!

Do you think it makes sense to pull it out of a note and just include it in line with the rest of the text if we're including an ipython?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah sure that sounds reasonable

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

I might be missing something here, but these days you can simply set dropna=False, which would include the NaNs



df = pd.DataFrame({"a": [1, np.nan, 2], "b": 3})

print(df.groupby("a", dropna=False).sum())

returns

     b
a     
1.0  3
2.0  3
NaN  3

@mroeschke
Copy link
Member

Good point @phofl. Looks we also have an example in the groupby docstring demonstrating this behavior so I don't think this workaround explanation is needed.

Thanks for the PR @aamnv but going to close since I think this old issue is not actionable anymore, but feel free to address any of the other good first issue issues!

@mroeschke mroeschke closed this Jun 14, 2022
@aamnv aamnv deleted the doc-update-groupby-na-handling branch June 14, 2022 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: update groupby NA group handing / workaround
3 participants