diff --git a/doc/source/reference/groupby.rst b/doc/source/reference/groupby.rst index ce9aeeb358c19..004651ac0074f 100644 --- a/doc/source/reference/groupby.rst +++ b/doc/source/reference/groupby.rst @@ -80,6 +80,7 @@ Function application DataFrameGroupBy.describe DataFrameGroupBy.diff DataFrameGroupBy.ewm + DataFrameGroupBy.expanding DataFrameGroupBy.ffill DataFrameGroupBy.first DataFrameGroupBy.head @@ -132,6 +133,7 @@ Function application SeriesGroupBy.describe SeriesGroupBy.diff SeriesGroupBy.ewm + SeriesGroupBy.expanding SeriesGroupBy.ffill SeriesGroupBy.first SeriesGroupBy.head diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 2cb523d2f2f55..7d58d8f867c12 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3803,16 +3803,58 @@ def rolling( ) @final - @Substitution(name="groupby") - @Appender(_common_see_also) def expanding(self, *args, **kwargs) -> ExpandingGroupby: """ - Return an expanding grouper, providing expanding - functionality per group. + 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. Returns ------- pandas.api.typing.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 + -------- + >>> 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() + 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