Skip to content

DOC: Added extra sentences to clarify series.GroupBy snippets in examples #59331

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 21 commits into from
Jul 31, 2024
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
28a7451
Added messages for each releveant snippet
ApoorvApoorv Jul 26, 2024
da61c6b
some small corrections to clarify further
ApoorvApoorv Jul 26, 2024
11c6b1a
removed trailing whitespace
ApoorvApoorv Jul 26, 2024
97095c9
more formatting correction
ApoorvApoorv Jul 26, 2024
d80dee9
more cleanup
ApoorvApoorv Jul 26, 2024
8f6adec
Merge branch 'main' into example-correction-groupby
ApoorvApoorv Jul 26, 2024
83385d1
reverting changes
ApoorvApoorv Jul 26, 2024
fe098c0
trying to format documentation correctly
ApoorvApoorv Jul 26, 2024
833620d
removed some part of addee text
ApoorvApoorv Jul 27, 2024
631f468
testing if removing list works
ApoorvApoorv Jul 27, 2024
2e13044
reverting some changes
ApoorvApoorv Jul 27, 2024
3b408ca
reverting changes
ApoorvApoorv Jul 27, 2024
ee1544c
checking if minor changes also leads to failures
ApoorvApoorv Jul 27, 2024
144e862
reverting all changes to pass the tests
ApoorvApoorv Jul 28, 2024
e3c9efa
checking is small changes causes errors as well
ApoorvApoorv Jul 28, 2024
f879523
Merge branch 'main' into example-correction-groupby
ApoorvApoorv Jul 28, 2024
55f86c2
Merge branch 'main' into example-correction-groupby
ApoorvApoorv Jul 30, 2024
3fce182
Merge branch 'main' into example-correction-groupby
ApoorvApoorv Jul 30, 2024
d448415
pusing the changes back
ApoorvApoorv Jul 30, 2024
cb2a76f
Merge branch 'main' into example-correction-groupby
ApoorvApoorv Jul 30, 2024
da186c1
Merge branch 'main' into example-correction-groupby
ApoorvApoorv Jul 30, 2024
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
26 changes: 26 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,14 +1815,30 @@ def _set_name(
Parrot 30.0
Parrot 20.0
Name: Max Speed, dtype: float64

We can pass a list of values to group the Series data by custom labels:

>>> ser.groupby(["a", "b", "a", "b"]).mean()
a 210.0
b 185.0
Name: Max Speed, dtype: float64

Grouping by numeric labels yields similar results:

>>> ser.groupby([0, 1, 0, 1]).mean()
0 210.0
1 185.0
Name: Max Speed, dtype: float64

We can group by a level of the index:

>>> ser.groupby(level=0).mean()
Falcon 370.0
Parrot 25.0
Name: Max Speed, dtype: float64

We can group by a condition applied to the Series values:

>>> ser.groupby(ser > 100).mean()
Max Speed
False 25.0
Expand All @@ -1845,11 +1861,16 @@ def _set_name(
Parrot Captive 30.0
Wild 20.0
Name: Max Speed, dtype: float64

>>> ser.groupby(level=0).mean()
Animal
Falcon 370.0
Parrot 25.0
Name: Max Speed, dtype: float64

We can also group by the 'Type' level of the hierarchical index
to get the mean speed for each type:

>>> ser.groupby(level="Type").mean()
Type
Captive 210.0
Expand All @@ -1865,12 +1886,17 @@ def _set_name(
b 3
dtype: int64

To include `NA` values in the group keys, set `dropna=False`:

>>> ser.groupby(level=0, dropna=False).sum()
a 3
b 3
NaN 3
dtype: int64

We can also group by a custom list with NaN values to handle
missing group labels:

>>> arrays = ['Falcon', 'Falcon', 'Parrot', 'Parrot']
>>> ser = pd.Series([390., 350., 30., 20.], index=arrays, name="Max Speed")
>>> ser.groupby(["a", "b", "a", np.nan]).mean()
Expand Down
Loading