Skip to content

DOC: Improve groupby().ngroup() explanation for missing groups #50049

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 4 commits into from
Jan 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3212,6 +3212,9 @@ def ngroup(self, ascending: bool = True):
would be seen when iterating over the groupby object, not the
order they are first observed.

Groups with missing keys (where `pd.isna()` is True) will be labeled with `NaN`
and will be skipped from the count.

Parameters
----------
ascending : bool, default True
Expand All @@ -3228,38 +3231,38 @@ def ngroup(self, ascending: bool = True):

Examples
--------
>>> df = pd.DataFrame({"A": list("aaabba")})
>>> df = pd.DataFrame({"color": ["red", None, "red", "blue", "blue", "red"]})
>>> df
A
0 a
1 a
2 a
3 b
4 b
5 a
>>> df.groupby('A').ngroup()
0 0
1 0
2 0
3 1
4 1
5 0
dtype: int64
>>> df.groupby('A').ngroup(ascending=False)
color
0 red
1 None
2 red
3 blue
4 blue
5 red
>>> df.groupby("color").ngroup()
0 1.0
1 NaN
2 1.0
3 0.0
4 0.0
5 1.0
dtype: float64
>>> df.groupby("color", dropna=False).ngroup()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dropped the ascending example, I figured that was obvious enough. Think I should keep it in?

Copy link
Member

@rhshadrach rhshadrach Jan 3, 2023

Choose a reason for hiding this comment

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

I think we typically demonstrate all the features of a method (ignoring methods with a large amount of arguments), regardless of how obvious they might seem. I think it should be retained.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I restored

        >>> df.groupby("color", dropna=False).ngroup(ascending=False)
        0    1
        1    0
        2    1
        3    2
        4    2
        5    1
        dtype: int64

I chose to use dropna=False because I wanted to show
that NA keys are placed BEFORE other keys.

I figured the dropna=True example was obvious enough from this and
I didn't need that one as well.

0 1
1 1
1 2
2 1
3 0
4 0
5 1
dtype: int64
>>> df.groupby(["A", [1,1,2,3,2,1]]).ngroup()
0 0
>>> df.groupby("color", dropna=False).ngroup(ascending=False)
0 1
1 0
2 1
3 3
3 2
4 2
5 0
5 1
dtype: int64
"""
with self._group_selection_context():
Expand Down