Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/reference/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Function application
DataFrameGroupBy.cumsum
DataFrameGroupBy.describe
DataFrameGroupBy.diff
DataFrameGroupBy.expanding
DataFrameGroupBy.ffill
DataFrameGroupBy.first
DataFrameGroupBy.head
Expand Down Expand Up @@ -130,6 +131,7 @@ Function application
SeriesGroupBy.cumsum
SeriesGroupBy.describe
SeriesGroupBy.diff
SeriesGroupBy.expanding
SeriesGroupBy.ffill
SeriesGroupBy.first
SeriesGroupBy.head
Expand Down
53 changes: 48 additions & 5 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3803,16 +3803,59 @@ 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.

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.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
--------
>>> 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

Expand Down