Skip to content

Commit 53ac271

Browse files
committed
improvements
1 parent 21cbbc0 commit 53ac271

File tree

1 file changed

+12
-36
lines changed

1 file changed

+12
-36
lines changed

pandas/core/groupby/groupby.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,8 +3828,7 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby:
38283828
@Appender(_common_see_also)
38293829
def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
38303830
"""
3831-
Return an exponential weighted moving average grouper,
3832-
providing ewm functionality per group.
3831+
Provide exponential weighted functions for the groupby.
38333832
38343833
Parameters
38353834
----------
@@ -3838,54 +3837,34 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
38383837
:math:`\\alpha = 1 / (1 + com)` for :math:`com \\geq 0`.
38393838
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must
38403839
be provided.
3841-
38423840
span : float, optional
38433841
Specify decay in terms of span:
38443842
:math:`\\alpha = 2 / (span + 1)` for :math:`span \\geq 1`.
38453843
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must
38463844
be provided.
3847-
38483845
halflife : float, optional
38493846
Specify decay in terms of half-life:
38503847
:math:`\\alpha = 1 - \\exp(-\\ln(2) / halflife)` for :math:`halflife > 0`.
38513848
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must
38523849
be provided.
3853-
38543850
alpha : float, optional
3855-
Specify the smoothing factor
3856-
:math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`.
3857-
3851+
Specify the smoothing factor :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`.
38583852
One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must
38593853
be provided.
3860-
38613854
min_periods : int, default 0
38623855
Minimum number of observations in window required to have a value;
38633856
otherwise, result is ``np.nan``.
3864-
38653857
adjust : bool, default True
38663858
Divide by decaying adjustment factor in beginning periods to account
38673859
for imbalance in relative weightings (viewing EWMA as a moving average).
3868-
3869-
If ``False``, the exponentially weighted function is:
3870-
3871-
.. math::
3872-
y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t
3873-
3874-
If ``True``, the exponentially weighted function is:
3875-
3876-
.. math::
3877-
y_t = \\frac{\\sum_{i=0}^t w_i x_{t-i}}{\\sum_{i=0}^t w_i}
3878-
3879-
where :math:`w_i = (1 - \\alpha)^i`.
3880-
38813860
ignore_na : bool, default False
38823861
If ``True``, missing values are ignored in the calculation.
3883-
38843862
If ``False``, missing values are treated as missing.
3885-
38863863
axis : {0 or 'index', 1 or 'columns'}, default 0
38873864
The axis to use. The value 0 identifies the rows, and 1 identifies the
38883865
columns.
3866+
*args, **kwargs
3867+
Additional arguments and keyword arguments passed to the function.
38893868
38903869
Returns
38913870
-------
@@ -3897,15 +3876,12 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
38973876
Each group is treated independently, and the exponential weighted calculations
38983877
are applied separately to each group.
38993878
3900-
The exponential weighted calculation is based on the formula:
3901-
3902-
.. math::
3903-
y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t
3879+
When ``adjust=True``, weighted averages are calculated using weights
3880+
:math:`w_i = (1-\\alpha)^i` where :math:`i` is the number of periods from the
3881+
observations being weighted to the current period.
39043882
3905-
where :math:`\\alpha` is the smoothing factor derived from one of the input
3906-
decay parameters (``com``, ``span``, ``halflife``, or ``alpha``).
3907-
3908-
Only one of ``com``, ``span``, ``halflife``, or ``alpha`` can be specified.
3883+
When ``adjust=False``, the calculation follows the recursive formula:
3884+
:math:`y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t`.
39093885
39103886
Examples
39113887
--------
@@ -3916,7 +3892,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
39163892
... }
39173893
... )
39183894
>>> df
3919-
Class Value
3895+
Class Value
39203896
0 A 10
39213897
1 A 20
39223898
2 A 30
@@ -3925,7 +3901,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
39253901
5 B 60
39263902
39273903
>>> df.groupby("Class").ewm(span=2).mean()
3928-
Value
3904+
Value
39293905
Class
39303906
A 0 10.000000
39313907
1 17.500000
@@ -3935,7 +3911,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
39353911
5 56.153846
39363912
39373913
>>> df.groupby("Class").ewm(alpha=0.5, adjust=False).mean()
3938-
Value
3914+
Value
39393915
Class
39403916
A 0 10.0
39413917
1 15.0

0 commit comments

Comments
 (0)