Skip to content

DOC: Add documentation for groupby.expanding() #61274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 14, 2025
2 changes: 2 additions & 0 deletions doc/source/reference/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Function application
DataFrameGroupBy.describe
DataFrameGroupBy.diff
DataFrameGroupBy.ewm
DataFrameGroupBy.expanding
DataFrameGroupBy.ffill
DataFrameGroupBy.first
DataFrameGroupBy.head
Expand Down Expand Up @@ -132,6 +133,7 @@ Function application
SeriesGroupBy.describe
SeriesGroupBy.diff
SeriesGroupBy.ewm
SeriesGroupBy.expanding
SeriesGroupBy.ffill
SeriesGroupBy.first
SeriesGroupBy.head
Expand Down
50 changes: 46 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down