Skip to content

DOC: Add Notes and Examples back to GroupBy.apply docs template #40207

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 3 commits into from
Mar 4, 2021
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
40 changes: 19 additions & 21 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,23 @@ class providing the base-class of operations.
transform : Apply function column-by-column to the GroupBy object.
Series.apply : Apply a function to a Series.
DataFrame.apply : Apply a function to each row or column of a DataFrame.

Notes
-----
In the current implementation `apply` calls `func` twice on the
first group to decide whether it can take a fast or slow code
path. This can lead to unexpected behavior if `func` has
side-effects, as they will take effect twice for the first
group.

Examples
--------
{examples}
""",
"dataframe_examples": """
>>> df = pd.DataFrame({'A': 'a a b'.split(),
'B': [1,2,3],
'C': [4,6, 5]})
... 'B': [1,2,3],
... 'C': [4,6, 5]})
>>> g = df.groupby('A')

Notice that ``g`` has two groups, ``a`` and ``b``.
Expand Down Expand Up @@ -192,8 +204,7 @@ class providing the base-class of operations.
A
a 5
b 2
dtype: int64
""",
dtype: int64""",
"series_examples": """
>>> s = pd.Series([0, 1, 2], index='a a b'.split())
>>> g = s.groupby(s.index)
Expand All @@ -206,9 +217,9 @@ class providing the base-class of operations.
each group together into a new Series:

>>> g.apply(lambda x: x*2 if x.name == 'b' else x/2)
0 0.0
1 0.5
2 4.0
a 0.0
a 0.5
b 4.0
dtype: float64

Example 2: The function passed to `apply` takes a Series as
Expand All @@ -219,20 +230,7 @@ class providing the base-class of operations.
>>> g.apply(lambda x: x.max() - x.min())
a 1
b 0
dtype: int64

Notes
-----
In the current implementation `apply` calls `func` twice on the
first group to decide whether it can take a fast or slow code
path. This can lead to unexpected behavior if `func` has
side-effects, as they will take effect twice for the first
group.

Examples
--------
{examples}
""",
dtype: int64""",
}

_groupby_agg_method_template = """
Expand Down