From c12bbe0efad653234d44695a4f79be65d8dab320 Mon Sep 17 00:00:00 2001 From: arthurlw Date: Fri, 11 Apr 2025 00:15:40 -0700 Subject: [PATCH 01/10] Added docstring and to groupby.rst --- doc/source/reference/groupby.rst | 2 ++ pandas/core/groupby/groupby.py | 43 ++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/doc/source/reference/groupby.rst b/doc/source/reference/groupby.rst index fc180c8161a7e..7561422dab49b 100644 --- a/doc/source/reference/groupby.rst +++ b/doc/source/reference/groupby.rst @@ -79,6 +79,7 @@ Function application DataFrameGroupBy.cumsum DataFrameGroupBy.describe DataFrameGroupBy.diff + DataFrameGroupBy.expanding DataFrameGroupBy.ffill DataFrameGroupBy.first DataFrameGroupBy.head @@ -130,6 +131,7 @@ Function application SeriesGroupBy.cumsum SeriesGroupBy.describe SeriesGroupBy.diff + SeriesGroupBy.expanding SeriesGroupBy.ffill SeriesGroupBy.first SeriesGroupBy.head diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 9cfeb53821fbc..13713a7b5ed4e 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3807,12 +3807,51 @@ def rolling( @Appender(_common_see_also) def expanding(self, *args, **kwargs) -> ExpandingGroupby: """ - Return an expanding grouper, providing expanding + Return an expanding grouper, providing expanding (cumulative) functionality per group. + Parameters + ---------- + min_periods : int, default 1 + Minimum number of observations in the window required to have a value; + otherwise, the result is ``np.nan``. + Returns ------- - pandas.api.typing.ExpandingGroupby + pandas.core.window.ExpandingGroupby + An object that supports expanding transformations over each group. + + See Also + -------- + Series.expanding : Expanding transformations for Series. + DataFrame.expanding : Expanding transformations for DataFrames. + Series.groupby : Apply a function groupby to a Series. + DataFrame.groupby : Apply a function groupby. + + Examples + -------- + >>> import pandas as pd + >>> df = pd.DataFrame({ + ... "Class": ["A", "A", "A", "B", "B", "B"], + ... "Value": [10, 20, 30, 40, 50, 60] + ... }) + >>> df + Class Value + 0 A 10 + 1 A 20 + 2 A 30 + 3 B 40 + 4 B 50 + 5 B 60 + + >>> df.groupby("Class").expanding().mean().reset_index(drop=True) + Value + 0 10.0 + 1 15.0 + 2 20.0 + 3 40.0 + 4 45.0 + 5 50.0 """ from pandas.core.window import ExpandingGroupby From e07c81bd789aeb76920f275fc6d3d0e0f7b4e443 Mon Sep 17 00:00:00 2001 From: arthurlw Date: Fri, 11 Apr 2025 17:04:21 -0700 Subject: [PATCH 02/10] removed annotations --- pandas/core/groupby/groupby.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 13713a7b5ed4e..59611ccb0151e 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3803,8 +3803,6 @@ def rolling( ) @final - @Substitution(name="groupby") - @Appender(_common_see_also) def expanding(self, *args, **kwargs) -> ExpandingGroupby: """ Return an expanding grouper, providing expanding (cumulative) From 4cdcfb99cef6ebe355d52bf6cfac7a01aaa37589 Mon Sep 17 00:00:00 2001 From: arthurlw Date: Fri, 11 Apr 2025 17:15:32 -0700 Subject: [PATCH 03/10] precommit --- pandas/core/groupby/groupby.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 59611ccb0151e..67f935be7c16a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3829,10 +3829,12 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: Examples -------- >>> import pandas as pd - >>> df = pd.DataFrame({ - ... "Class": ["A", "A", "A", "B", "B", "B"], - ... "Value": [10, 20, 30, 40, 50, 60] - ... }) + >>> df = pd.DataFrame( + ... { + ... "Class": ["A", "A", "A", "B", "B", "B"], + ... "Value": [10, 20, 30, 40, 50, 60], + ... } + ... ) >>> df Class Value 0 A 10 From e5b3656f63cfa922ae1b6af1a7542953bab40c6f Mon Sep 17 00:00:00 2001 From: arthurlw Date: Fri, 11 Apr 2025 17:55:46 -0700 Subject: [PATCH 04/10] updated parameters docstring --- pandas/core/groupby/groupby.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 67f935be7c16a..cb1680b464ab8 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3810,9 +3810,15 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: Parameters ---------- - min_periods : int, default 1 - Minimum number of observations in the window required to have a value; - otherwise, the result is ``np.nan``. + *args : tuple + Positional arguments passed to the expanding window constructor. + **kwargs : dict + Keyword arguments passed to the expanding window constructor, + such as: + + min_periods : int, default 1 + Minimum number of observations in the window required to have a value; + otherwise, the result is ``np.nan``. Returns ------- From 06eb37d64282ceef8fdb95a2ea8800452d5c46c9 Mon Sep 17 00:00:00 2001 From: arthurlw Date: Fri, 11 Apr 2025 18:18:54 -0700 Subject: [PATCH 05/10] shortened summary --- pandas/core/groupby/groupby.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index cb1680b464ab8..11c6d943a904b 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3805,7 +3805,7 @@ def rolling( @final def expanding(self, *args, **kwargs) -> ExpandingGroupby: """ - Return an expanding grouper, providing expanding (cumulative) + Return an expanding grouper, providing expanding functionality per group. Parameters @@ -3834,7 +3834,6 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: Examples -------- - >>> import pandas as pd >>> df = pd.DataFrame( ... { ... "Class": ["A", "A", "A", "B", "B", "B"], From 6b9799421dea40d0f16aec0374cb5a962ab870de Mon Sep 17 00:00:00 2001 From: arthurlw Date: Fri, 11 Apr 2025 19:08:01 -0700 Subject: [PATCH 06/10] summary --- pandas/core/groupby/groupby.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 11c6d943a904b..8acf4d82120ef 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3805,8 +3805,7 @@ def rolling( @final def expanding(self, *args, **kwargs) -> ExpandingGroupby: """ - Return an expanding grouper, providing expanding - functionality per group. + Return an expanding grouper, providing expanding functionality per group. Parameters ---------- From 51c58fe7a8642e50d7beb8fd5067a524ecb1277f Mon Sep 17 00:00:00 2001 From: arthurlw Date: Mon, 14 Apr 2025 11:21:15 -0700 Subject: [PATCH 07/10] updated according to reviewer suggestions --- pandas/core/groupby/groupby.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 8acf4d82120ef..295b6a2ec3056 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3807,21 +3807,19 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: """ Return an expanding grouper, providing expanding functionality per group. + Arguments are the same as `:meth:DataFrame.rolling` except that ``step`` cannot + be specified. + Parameters ---------- *args : tuple Positional arguments passed to the expanding window constructor. **kwargs : dict Keyword arguments passed to the expanding window constructor, - such as: - - min_periods : int, default 1 - Minimum number of observations in the window required to have a value; - otherwise, the result is ``np.nan``. Returns ------- - pandas.core.window.ExpandingGroupby + pandas.api.typing.ExpandingGroupby An object that supports expanding transformations over each group. See Also @@ -3848,7 +3846,7 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: 4 B 50 5 B 60 - >>> df.groupby("Class").expanding().mean().reset_index(drop=True) + >>> df.groupby("Class").expanding().mean() Value 0 10.0 1 15.0 From 5c91799a144f840cb64f312af135836dc6fc9eb1 Mon Sep 17 00:00:00 2001 From: arthurlw Date: Mon, 14 Apr 2025 11:45:29 -0700 Subject: [PATCH 08/10] Fixed docstring error --- pandas/core/groupby/groupby.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 295b6a2ec3056..d7d867cd0cc99 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3846,14 +3846,15 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: 4 B 50 5 B 60 - >>> df.groupby("Class").expanding().mean() - Value - 0 10.0 - 1 15.0 - 2 20.0 - 3 40.0 - 4 45.0 - 5 50.0 + >>> df.groupby("Class").expanding().mean()\ + Value + Class + A 0 10.0 + 1 15.0 + 2 20.0 + B 3 40.0 + 4 45.0 + 5 50.0 """ from pandas.core.window import ExpandingGroupby From 93eb7528342e0512f007f855099babea8de3a25b Mon Sep 17 00:00:00 2001 From: arthurlw Date: Mon, 14 Apr 2025 12:17:19 -0700 Subject: [PATCH 09/10] fixed docstring error --- 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 d7d867cd0cc99..9e77aac97266d 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3846,7 +3846,7 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: 4 B 50 5 B 60 - >>> df.groupby("Class").expanding().mean()\ + >>> df.groupby("Class").expanding().mean() Value Class A 0 10.0 From 7daa3c0b937647f437d7484909e0a42b9da02126 Mon Sep 17 00:00:00 2001 From: arthurlw Date: Mon, 14 Apr 2025 13:58:12 -0700 Subject: [PATCH 10/10] removed comma at the end of sentence --- 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 9e77aac97266d..0d11f1080b270 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3815,7 +3815,7 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: *args : tuple Positional arguments passed to the expanding window constructor. **kwargs : dict - Keyword arguments passed to the expanding window constructor, + Keyword arguments passed to the expanding window constructor. Returns -------