Skip to content

Commit 58a0b6f

Browse files
authored
DOC: Improve groupby().ngroup() explanation for missing groups (#50049)
1 parent 13143d9 commit 58a0b6f

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

pandas/core/groupby/groupby.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -3174,6 +3174,9 @@ def ngroup(self, ascending: bool = True):
31743174
would be seen when iterating over the groupby object, not the
31753175
order they are first observed.
31763176
3177+
Groups with missing keys (where `pd.isna()` is True) will be labeled with `NaN`
3178+
and will be skipped from the count.
3179+
31773180
Parameters
31783181
----------
31793182
ascending : bool, default True
@@ -3190,38 +3193,38 @@ def ngroup(self, ascending: bool = True):
31903193
31913194
Examples
31923195
--------
3193-
>>> df = pd.DataFrame({"A": list("aaabba")})
3196+
>>> df = pd.DataFrame({"color": ["red", None, "red", "blue", "blue", "red"]})
31943197
>>> df
3195-
A
3196-
0 a
3197-
1 a
3198-
2 a
3199-
3 b
3200-
4 b
3201-
5 a
3202-
>>> df.groupby('A').ngroup()
3203-
0 0
3204-
1 0
3205-
2 0
3206-
3 1
3207-
4 1
3208-
5 0
3209-
dtype: int64
3210-
>>> df.groupby('A').ngroup(ascending=False)
3198+
color
3199+
0 red
3200+
1 None
3201+
2 red
3202+
3 blue
3203+
4 blue
3204+
5 red
3205+
>>> df.groupby("color").ngroup()
3206+
0 1.0
3207+
1 NaN
3208+
2 1.0
3209+
3 0.0
3210+
4 0.0
3211+
5 1.0
3212+
dtype: float64
3213+
>>> df.groupby("color", dropna=False).ngroup()
32113214
0 1
3212-
1 1
3215+
1 2
32133216
2 1
32143217
3 0
32153218
4 0
32163219
5 1
32173220
dtype: int64
3218-
>>> df.groupby(["A", [1,1,2,3,2,1]]).ngroup()
3219-
0 0
3221+
>>> df.groupby("color", dropna=False).ngroup(ascending=False)
3222+
0 1
32203223
1 0
32213224
2 1
3222-
3 3
3225+
3 2
32233226
4 2
3224-
5 0
3227+
5 1
32253228
dtype: int64
32263229
"""
32273230
with self._group_selection_context():

0 commit comments

Comments
 (0)