From 0be8335a3577b0e68aaea14b8461bf2d2d6f5733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Tue, 1 Mar 2022 16:46:54 -0500 Subject: [PATCH 1/5] DOC: Clarify groupby.first does not use nulls --- pandas/core/groupby/groupby.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 36a6c531f7ab8..ce45af24e9b3c 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2235,8 +2235,37 @@ def max( ) @final - @doc(_groupby_agg_method_template, fname="first", no=False, mc=-1) + @Substitution(name="groupby") + @Appender(_common_see_also) def first(self, numeric_only: bool = False, min_count: int = -1): + """ + Compute the first non-null entry of each column. + + Parameters + ---------- + numeric_only : bool, default False + Include only float, int, boolean columns. If None, will attempt to use + everything, then use only numeric data. + min_count : int, default -1 + The required number of valid values to perform the operation. If fewer + than ``min_count`` non-NA values are present the result will be NA. + + Returns + ------- + Series or DataFrame + First not non-null of values within each group. + + Examples + -------- + >>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[None, 5, 6], C=[1, 2, 3])) + >>> df.groupby("A").first() + B C + A + 1 5.0 1 + 3 6.0 3 + + """ + def first_compat(obj: NDFrameT, axis: int = 0): def first(x: Series): """Helper function for first item that isn't NA.""" From b1cdb3edf7b2b9b83da23c6142be62e0927f4a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Fri, 4 Mar 2022 17:28:21 -0500 Subject: [PATCH 2/5] DOC: Clarify groupby.last does not use nulls --- pandas/core/groupby/groupby.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index ce45af24e9b3c..cf143641af68d 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2289,8 +2289,37 @@ def first(x: Series): ) @final - @doc(_groupby_agg_method_template, fname="last", no=False, mc=-1) + @Substitution(name="groupby") + @Appender(_common_see_also) def last(self, numeric_only: bool = False, min_count: int = -1): + """ + Compute the last non-null entry of each column. + + Parameters + ---------- + numeric_only : bool, default False + Include only float, int, boolean columns. If None, will attempt to use + everything, then use only numeric data. + min_count : int, default -1 + The required number of valid values to perform the operation. If fewer + than ``min_count`` non-NA values are present the result will be NA. + + Returns + ------- + Series or DataFrame + First not non-null of values within each group. + + Examples + -------- + >>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[5, None, 6], C=[1, 2, 3])) + >>> df.groupby("A").first() + B C + A + 1 5.0 2 + 3 6.0 3 + + """ + def last_compat(obj: NDFrameT, axis: int = 0): def last(x: Series): """Helper function for last item that isn't NA.""" From d148f5ffea0243f5129f570bf61e62f4c1fe901e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Sat, 5 Mar 2022 08:51:24 -0500 Subject: [PATCH 3/5] DOC: Correct groupby.last does not use nulls --- pandas/core/groupby/groupby.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index cf143641af68d..4e1d148297d3b 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2307,12 +2307,12 @@ def last(self, numeric_only: bool = False, min_count: int = -1): Returns ------- Series or DataFrame - First not non-null of values within each group. + Last not non-null of values within each group. Examples -------- >>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[5, None, 6], C=[1, 2, 3])) - >>> df.groupby("A").first() + >>> df.groupby("A").last() B C A 1 5.0 2 From 6274e1657adbbf823bcb56358d11aaba451ad259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Sat, 5 Mar 2022 10:17:46 -0500 Subject: [PATCH 4/5] DOC: Correct first and last docstring See Also --- pandas/core/groupby/groupby.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 4e1d148297d3b..373632053a7aa 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2236,7 +2236,6 @@ def max( @final @Substitution(name="groupby") - @Appender(_common_see_also) def first(self, numeric_only: bool = False, min_count: int = -1): """ Compute the first non-null entry of each column. @@ -2255,6 +2254,14 @@ def first(self, numeric_only: bool = False, min_count: int = -1): Series or DataFrame First not non-null of values within each group. + See Also + -------- + DataFrame.groupby : Apply a function groupby to each row or column of a + DataFrame. + DataFrame.core.groupby.GroupBy.last : Compute the last non-null entry of each + column. + DataFrame.core.groupby.GroupBy.nth : Take the nth row from each group. + Examples -------- >>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[None, 5, 6], C=[1, 2, 3])) @@ -2263,7 +2270,6 @@ def first(self, numeric_only: bool = False, min_count: int = -1): A 1 5.0 1 3 6.0 3 - """ def first_compat(obj: NDFrameT, axis: int = 0): @@ -2290,7 +2296,6 @@ def first(x: Series): @final @Substitution(name="groupby") - @Appender(_common_see_also) def last(self, numeric_only: bool = False, min_count: int = -1): """ Compute the last non-null entry of each column. @@ -2309,6 +2314,14 @@ def last(self, numeric_only: bool = False, min_count: int = -1): Series or DataFrame Last not non-null of values within each group. + See Also + -------- + DataFrame.groupby : Apply a function groupby to each row or column of a + DataFrame. + DataFrame.core.groupby.GroupBy.first : Compute the first non-null entry of each + column. + DataFrame.core.groupby.GroupBy.nth : Take the nth row from each group. + Examples -------- >>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[5, None, 6], C=[1, 2, 3])) From e4447f732a70c24dcc9351e619a50cb6f140f2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Tue, 8 Mar 2022 15:29:51 -0500 Subject: [PATCH 5/5] DOC: Correct groupby first and last docstrings --- pandas/core/groupby/groupby.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 373632053a7aa..3d857e4f3e4e7 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2252,7 +2252,7 @@ def first(self, numeric_only: bool = False, min_count: int = -1): Returns ------- Series or DataFrame - First not non-null of values within each group. + First non-null of values within each group. See Also -------- @@ -2312,7 +2312,7 @@ def last(self, numeric_only: bool = False, min_count: int = -1): Returns ------- Series or DataFrame - Last not non-null of values within each group. + Last non-null of values within each group. See Also -------- @@ -2330,7 +2330,6 @@ def last(self, numeric_only: bool = False, min_count: int = -1): A 1 5.0 2 3 6.0 3 - """ def last_compat(obj: NDFrameT, axis: int = 0):