w3resource

Pandas: Series - ewm() function

Exponential weighted functions in Pandas

The ewm() function is used to provide exponential weighted functions.

Syntax:

Series.ewm(self, com=None, span=None, halflife=None, alpha=None, min_periods=0, adjust=True, ignore_na=False, axis=0)
Pandas Series ewm image

Parameters:

Name Description Type/Default Value Required / Optional
com Specify decay in terms of center of mass, α=1/(1+com), for com≥0. float Optional
span Specify decay in terms of span, α=2/(span+1), for span≥1. float Optional
halflife Specify decay in terms of half-life, α=1−exp(log(0.5)/halflife),forhalflife>0. float Optional
alpha  Specify smoothing factor α directly, 0<α≤1 float Optional
min_periods Minimum number of observations in window required to have a value (otherwise result is NA). int
Default Value: 0
Required
adjust Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average) bool
Default Value: True
Required
ignore_na  Ignore missing values when calculating weights; specify True to reproduce pre-0.15.0 behavior. bool
Default Value: False
Required
axis The axis to use. The value 0 identifies the rows, and 1 identifies the columns.  {0 or ‘index’, 1 or ‘columns’}
Default Value: 0
Required

Returns: DataFrame
A Window sub-classed for the particular operation.

Notes: Exactly one of center of mass, span, half-life, and alpha must be provided. Allowed values and relationship between the parameters are specified in the parameter descriptions above; see the link at the end of this section for a detailed explanation.
When adjust is True (default), weighted averages are calculated using weights (1-alpha)**(n-1), (1-alpha)**(n-2), …, 1-alpha, 1.
When adjust is False, weighted averages are calculated recursively as:
weighted_average[0] = arg[0]; weighted_average[i] = (1-alpha)*weighted_average[i-1] + alpha*arg[i].
When ignore_na is False (default), weights are based on absolute positions. For example, the weights of x and y used in calculating the final weighted average of [x, None, y] are (1-alpha)**2 and 1 (if adjust is True), and (1-alpha)**2 and alpha (if adjust is False).
When ignore_na is True (reproducing pre-0.15.0 behavior), weights are based on relative positions. For example, the weights of x and y used in calculating the final weighted average of [x, None, y] are 1-alpha and 1 (if adjust is True), and 1-alpha and alpha (if adjust is False).

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'Q': [0, 2, 4, np.nan, 6]})
df

Output:

    Q
0	0.0
1	2.0
2	4.0
3	NaN
4	6.0
Pandas Series ewm image

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'Q': [0, 2, 4, np.nan, 6]})
df.ewm(com=0.3).mean()

Output:

    Q
0	0.000000
1	1.625000
2	3.474654
3	3.474654
4	5.838369

Previous: Expanding transformations in Pandas
Next: Pandas absolute value of column



Follow us on Facebook and Twitter for latest update.