Skip to content

Commit 337254f

Browse files
committed
DOC: Add documentation for groupby.ewm()
1 parent 38dd653 commit 337254f

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

doc/source/reference/groupby.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Function application
7979
DataFrameGroupBy.cumsum
8080
DataFrameGroupBy.describe
8181
DataFrameGroupBy.diff
82+
DataFrameGroupBy.ewm
8283
DataFrameGroupBy.ffill
8384
DataFrameGroupBy.first
8485
DataFrameGroupBy.head
@@ -130,6 +131,7 @@ Function application
130131
SeriesGroupBy.cumsum
131132
SeriesGroupBy.describe
132133
SeriesGroupBy.diff
134+
SeriesGroupBy.ewm
133135
SeriesGroupBy.ffill
134136
SeriesGroupBy.first
135137
SeriesGroupBy.head

pandas/core/groupby/groupby.py

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3826,14 +3826,148 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby:
38263826
@final
38273827
@Substitution(name="groupby")
38283828
@Appender(_common_see_also)
3829-
def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
3829+
def ewm(
3830+
self,
3831+
com: float | None = None,
3832+
span: float | None = None,
3833+
halflife: float | None = None,
3834+
alpha: float | None = None,
3835+
min_periods: int = 0,
3836+
adjust: bool = True,
3837+
ignore_na: bool = False,
3838+
axis: Axis = 0,
3839+
) -> ExponentialMovingWindowGroupby:
38303840
"""
3831-
Return an ewm grouper, providing ewm functionality per group.
3841+
Return an exponential weighted moving average grouper, providing ewm functionality per group.
3842+
3843+
Parameters
3844+
----------
3845+
com : float, optional
3846+
Specify decay in terms of center of mass:
3847+
:math:`\\alpha = 1 / (1 + com)` for :math:`com \\geq 0`.
3848+
3849+
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided.
3850+
3851+
span : float, optional
3852+
Specify decay in terms of span:
3853+
:math:`\\alpha = 2 / (span + 1)` for :math:`span \\geq 1`.
3854+
3855+
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided.
3856+
3857+
halflife : float, optional
3858+
Specify decay in terms of half-life:
3859+
:math:`\\alpha = 1 - \\exp(-\\ln(2) / halflife)` for :math:`halflife > 0`.
3860+
3861+
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided.
3862+
3863+
alpha : float, optional
3864+
Specify the smoothing factor :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`.
3865+
3866+
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided.
3867+
3868+
min_periods : int, default 0
3869+
Minimum number of observations in window required to have a value;
3870+
otherwise, result is ``np.nan``.
3871+
3872+
adjust : bool, default True
3873+
Divide by decaying adjustment factor in beginning periods to account
3874+
for imbalance in relative weightings (viewing EWMA as a moving average).
3875+
3876+
If ``False``, the exponentially weighted function is:
3877+
3878+
.. math::
3879+
y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t
3880+
3881+
If ``True``, the exponentially weighted function is:
3882+
3883+
.. math::
3884+
y_t = \\frac{\\sum_{i=0}^t w_i x_{t-i}}{\\sum_{i=0}^t w_i}
3885+
3886+
where :math:`w_i = (1 - \\alpha)^i`.
3887+
3888+
ignore_na : bool, default False
3889+
If ``True``, missing values are ignored in the calculation.
3890+
3891+
If ``False``, missing values are treated as missing.
3892+
3893+
axis : {0 or 'index', 1 or 'columns'}, default 0
3894+
The axis to use. The value 0 identifies the rows, and 1 identifies the columns.
38323895
38333896
Returns
38343897
-------
38353898
pandas.api.typing.ExponentialMovingWindowGroupby
3899+
Return a new grouper with exponential moving window capabilities.
3900+
3901+
See Also
3902+
--------
3903+
Series.ewm : Exponential weighted moving window functions for Series.
3904+
DataFrame.ewm : Exponential weighted moving window functions for DataFrames.
3905+
Series.groupby : Apply a function groupby to a Series.
3906+
DataFrame.groupby : Apply a function groupby to a DataFrame.
3907+
3908+
Notes
3909+
-----
3910+
Each group is treated independently, and the exponential weighted calculations
3911+
are applied separately to each group.
3912+
3913+
The exponential weighted calculation is based on the formula:
3914+
3915+
.. math::
3916+
y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t
3917+
3918+
where :math:`\\alpha` is the smoothing factor derived from one of the input
3919+
decay parameters (``com``, ``span``, ``halflife``, or ``alpha``).
3920+
3921+
Only one of ``com``, ``span``, ``halflife``, or ``alpha`` can be specified.
3922+
3923+
Examples
3924+
--------
3925+
>>> df = pd.DataFrame(
3926+
... {
3927+
... "Class": ["A", "A", "A", "B", "B", "B"],
3928+
... "Value": [10, 20, 30, 40, 50, 60],
3929+
... }
3930+
... )
3931+
>>> df
3932+
Class Value
3933+
0 A 10
3934+
1 A 20
3935+
2 A 30
3936+
3 B 40
3937+
4 B 50
3938+
5 B 60
3939+
3940+
>>> df.groupby("Class").ewm(span=2).mean()
3941+
Value
3942+
Class
3943+
A 0 10.000000
3944+
1 17.500000
3945+
2 26.153846
3946+
B 3 40.000000
3947+
4 47.500000
3948+
5 56.153846
3949+
3950+
>>> df.groupby("Class").ewm(alpha=0.5, adjust=False).mean()
3951+
Value
3952+
Class
3953+
A 0 10.000000
3954+
1 15.000000
3955+
2 22.500000
3956+
B 3 40.000000
3957+
4 45.000000
3958+
5 52.500000
3959+
3960+
>>> df.groupby("Class").ewm(com=1.0, min_periods=1).std()
3961+
Value
3962+
Class
3963+
A 0 0.000000
3964+
1 7.500000
3965+
2 10.606602
3966+
B 3 0.000000
3967+
4 7.500000
3968+
5 10.606602
38363969
"""
3970+
38373971
from pandas.core.window import ExponentialMovingWindowGroupby
38383972

38393973
return ExponentialMovingWindowGroupby(

0 commit comments

Comments
 (0)