From adbdcb1719dd1dcfcc0f4e0aec06dfbff14a68dc Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 6 Jul 2021 17:39:42 +0530 Subject: [PATCH 1/8] DOC: Adding examples to DataFrameGroupBy.rank #38972 --- pandas/core/groupby/groupby.py | 44 +++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index a3a894560edfd..832971cb32a5b 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2641,7 +2641,6 @@ def cumcount(self, ascending: bool = True): @final @Substitution(name="groupby") - @Appender(_common_see_also) def rank( self, method: str = "average", @@ -2675,6 +2674,49 @@ def rank( Returns ------- DataFrame with ranking of values within each group + + See Also + -------- + Series.groupby : Apply a function groupby to a Series. + DataFrame.groupby : Apply a function groupby + to each row or column of a DataFrame. + Series.rank : Apply a function rank to a Series. + DataFrame.rank : Apply a function rank + to each row or column of a DataFrame. + + Examples + -------- + >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a'], + ... 'value': [.2, .4, .2, 0.01, .3, .11, .21, .4, .01, 0.2]}) + >>> df + group value + 0 a 0.20 + 1 a 0.40 + 2 a 0.20 + 3 b 0.01 + 4 a 0.30 + 5 b 0.11 + 6 b 0.21 + 7 b 0.40 + 8 b 0.01 + 9 a 0.20 + >>> df['average_rank'] = df.groupby('group')['value'].rank('average') + >>> df['min_rank'] = df.groupby('group')['value'].rank('min') + >>> df['max_rank'] = df.groupby('group')['value'].rank('max') + >>> df['dense_rank'] = df.groupby('group')['value'].rank('dense') + >>> df['first_rank'] = df.groupby('group')['value'].rank('first') + >>> df + group value average_rank min_rank max_rank dense_rank first_rank + 0 a 0.20 2.0 1.0 3.0 1.0 1.0 + 1 a 0.40 5.0 5.0 5.0 3.0 5.0 + 2 a 0.20 2.0 1.0 3.0 1.0 2.0 + 3 b 0.01 1.5 1.0 2.0 1.0 1.0 + 4 a 0.30 4.0 4.0 4.0 2.0 4.0 + 5 b 0.11 3.0 3.0 3.0 2.0 3.0 + 6 b 0.21 4.0 4.0 4.0 3.0 4.0 + 7 b 0.40 5.0 5.0 5.0 4.0 5.0 + 8 b 0.01 1.5 1.0 2.0 1.0 2.0 + 9 a 0.20 2.0 1.0 3.0 1.0 3.0 """ if na_option not in {"keep", "top", "bottom"}: msg = "na_option must be one of 'keep', 'top', or 'bottom'" From ff713905ca6ab6ec3b02da9365c35b6a78529086 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 6 Jul 2021 18:11:20 +0530 Subject: [PATCH 2/8] DOC: Adding examples to DataFrameGroupBy.rank #38972 --- pandas/core/groupby/groupby.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 832971cb32a5b..3355c725273e1 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2686,8 +2686,10 @@ def rank( Examples -------- - >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a'], - ... 'value': [.2, .4, .2, 0.01, .3, .11, .21, .4, .01, 0.2]}) + >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'b', + ... 'a', 'b', 'b', 'b', 'b', 'a'], + ... 'value': [.2, .4, .2, 0.01, + ... .3, .11, .21, .4, .01, 0.2]}) >>> df group value 0 a 0.20 From ab42746b45453284f4281e8b68e1601a6c5bcd9f Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 6 Jul 2021 21:28:47 +0530 Subject: [PATCH 3/8] DOC: made the suggested changes --- pandas/core/groupby/groupby.py | 54 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 3355c725273e1..1bdca28794230 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2686,39 +2686,35 @@ def rank( Examples -------- - >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'b', - ... 'a', 'b', 'b', 'b', 'b', 'a'], - ... 'value': [.2, .4, .2, 0.01, - ... .3, .11, .21, .4, .01, 0.2]}) + >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'a', + ... 'a', 'b', 'b', 'b', 'b', 'b'], + ... 'value': [2, 4, 2, 3, 5, 1, 2, 4, 1, 5]}) >>> df group value - 0 a 0.20 - 1 a 0.40 - 2 a 0.20 - 3 b 0.01 - 4 a 0.30 - 5 b 0.11 - 6 b 0.21 - 7 b 0.40 - 8 b 0.01 - 9 a 0.20 - >>> df['average_rank'] = df.groupby('group')['value'].rank('average') - >>> df['min_rank'] = df.groupby('group')['value'].rank('min') - >>> df['max_rank'] = df.groupby('group')['value'].rank('max') - >>> df['dense_rank'] = df.groupby('group')['value'].rank('dense') - >>> df['first_rank'] = df.groupby('group')['value'].rank('first') + 0 a 2 + 1 a 4 + 2 a 2 + 3 a 3 + 4 a 5 + 5 b 1 + 6 b 2 + 7 b 4 + 8 b 1 + 9 b 5 + >>> for method in ['average', 'min', 'max', 'dense', 'first']: + ... df[f'{method}_rank'] = df.groupby('group')['value'].rank(method) >>> df group value average_rank min_rank max_rank dense_rank first_rank - 0 a 0.20 2.0 1.0 3.0 1.0 1.0 - 1 a 0.40 5.0 5.0 5.0 3.0 5.0 - 2 a 0.20 2.0 1.0 3.0 1.0 2.0 - 3 b 0.01 1.5 1.0 2.0 1.0 1.0 - 4 a 0.30 4.0 4.0 4.0 2.0 4.0 - 5 b 0.11 3.0 3.0 3.0 2.0 3.0 - 6 b 0.21 4.0 4.0 4.0 3.0 4.0 - 7 b 0.40 5.0 5.0 5.0 4.0 5.0 - 8 b 0.01 1.5 1.0 2.0 1.0 2.0 - 9 a 0.20 2.0 1.0 3.0 1.0 3.0 + 0 a 2 1.5 1.0 2.0 1.0 1.0 + 1 a 4 4.0 4.0 4.0 3.0 4.0 + 2 a 2 1.5 1.0 2.0 1.0 2.0 + 3 a 3 3.0 3.0 3.0 2.0 3.0 + 4 a 5 5.0 5.0 5.0 4.0 5.0 + 5 b 1 1.5 1.0 2.0 1.0 1.0 + 6 b 2 3.0 3.0 3.0 2.0 3.0 + 7 b 4 4.0 4.0 4.0 3.0 4.0 + 8 b 1 1.5 1.0 2.0 1.0 2.0 + 9 b 5 5.0 5.0 5.0 4.0 5.0 """ if na_option not in {"keep", "top", "bottom"}: msg = "na_option must be one of 'keep', 'top', or 'bottom'" From 1b2dde5f432d0c887831524dafed1a35b260f5f7 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Sun, 11 Jul 2021 12:31:40 +0530 Subject: [PATCH 4/8] DOC: changed as suggested --- 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 3a641bd21ad8e..22311c09a7bde 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2682,8 +2682,8 @@ def rank( Examples -------- - >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'a', - ... 'a', 'b', 'b', 'b', 'b', 'b'], + >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'a', 'a', + ... 'b', 'b', 'b', 'b', 'b'], ... 'value': [2, 4, 2, 3, 5, 1, 2, 4, 1, 5]}) >>> df group value From 81000b77b8e8b9a75248f49267c021a142c94308 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Sun, 11 Jul 2021 12:32:57 +0530 Subject: [PATCH 5/8] DOC: changed as suggested --- pandas/core/groupby/groupby.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 22311c09a7bde..02757052f9581 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2637,6 +2637,7 @@ def cumcount(self, ascending: bool = True): @final @Substitution(name="groupby") + @Appender(_common_see_also) def rank( self, method: str = "average", @@ -2671,15 +2672,6 @@ def rank( ------- DataFrame with ranking of values within each group - See Also - -------- - Series.groupby : Apply a function groupby to a Series. - DataFrame.groupby : Apply a function groupby - to each row or column of a DataFrame. - Series.rank : Apply a function rank to a Series. - DataFrame.rank : Apply a function rank - to each row or column of a DataFrame. - Examples -------- >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'a', 'a', From 98b90c68c887949ba3d49922568b2d8606c5119f Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Sun, 11 Jul 2021 12:41:22 +0530 Subject: [PATCH 6/8] DOC: updating with black output --- pandas/core/groupby/groupby.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 02757052f9581..a2a96362fd0c8 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2674,9 +2674,12 @@ def rank( Examples -------- - >>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'a', 'a', - ... 'b', 'b', 'b', 'b', 'b'], - ... 'value': [2, 4, 2, 3, 5, 1, 2, 4, 1, 5]}) + df = pd.DataFrame( + ... { + ... "group": ["a", "a", "a", "a", "a", "b", "b", "b", "b", "b"], + ... "value": [2, 4, 2, 3, 5, 1, 2, 4, 1, 5], + ... } + ... ) >>> df group value 0 a 2 From ed0883217cf867cac51f1d085151e645f55336ce Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Sun, 11 Jul 2021 12:47:29 +0530 Subject: [PATCH 7/8] DOC: updating with black output --- pandas/core/groupby/groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index a2a96362fd0c8..44d0474b857a9 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2674,7 +2674,7 @@ def rank( Examples -------- - df = pd.DataFrame( + >>> df = pd.DataFrame( ... { ... "group": ["a", "a", "a", "a", "a", "b", "b", "b", "b", "b"], ... "value": [2, 4, 2, 3, 5, 1, 2, 4, 1, 5], From 0032ae4e77f1d8462ffe0c43e6fa1853ad4686de Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Mon, 12 Jul 2021 01:10:36 +0530 Subject: [PATCH 8/8] DOC: corrected docstring order --- 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 44d0474b857a9..c5609672a9e5a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2637,7 +2637,7 @@ def cumcount(self, ascending: bool = True): @final @Substitution(name="groupby") - @Appender(_common_see_also) + @Substitution(see_also=_common_see_also) def rank( self, method: str = "average", @@ -2671,7 +2671,7 @@ def rank( Returns ------- DataFrame with ranking of values within each group - + %(see_also)s Examples -------- >>> df = pd.DataFrame(