From b6cab45c6f2b30aa0d9f627d8a8986bce53f9c32 Mon Sep 17 00:00:00 2001 From: Brian Hulette Date: Wed, 3 Mar 2021 12:09:21 -0800 Subject: [PATCH 1/3] Add Notes and Examples back to GroupBy.apply docs tempalte --- pandas/core/groupby/groupby.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index d8135dbf3f08d..6589ba9dc0643 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -153,6 +153,18 @@ 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(), @@ -220,18 +232,6 @@ class providing the base-class of operations. 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} """, } From 4f91af6c0258beee51571673cbba8b30cfa94320 Mon Sep 17 00:00:00 2001 From: Brian Hulette Date: Wed, 3 Mar 2021 13:29:00 -0800 Subject: [PATCH 2/3] Fix dupe newline --- pandas/core/groupby/groupby.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 6589ba9dc0643..272919d62008f 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -204,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) @@ -231,8 +230,7 @@ class providing the base-class of operations. >>> g.apply(lambda x: x.max() - x.min()) a 1 b 0 - dtype: int64 - """, + dtype: int64""", } _groupby_agg_method_template = """ From 20d52e056b501fe7a4b4f1726b631afef01a79df Mon Sep 17 00:00:00 2001 From: Brian Hulette Date: Wed, 3 Mar 2021 15:19:10 -0800 Subject: [PATCH 3/3] Fix doctests --- pandas/core/groupby/groupby.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 272919d62008f..e11c296783476 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -168,8 +168,8 @@ class providing the base-class of operations. """, "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``. @@ -217,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