DataFrame.
ewm
Provide exponential weighted (EW) functions.
Available EW functions: mean(), var(), std(), corr(), cov().
mean()
var()
std()
corr()
cov()
Exactly one parameter: com, span, halflife, or alpha must be provided.
com
span
halflife
alpha
Specify decay in terms of center of mass, \(\alpha = 1 / (1 + com)\), for \(com \geq 0\).
Specify decay in terms of span, \(\alpha = 2 / (span + 1)\), for \(span \geq 1\).
Specify decay in terms of half-life, \(\alpha = 1 - \exp\left(-\ln(2) / halflife\right)\), for \(halflife > 0\).
If times is specified, the time unit (str or timedelta) over which an observation decays to half its value. Only applicable to mean() and halflife value will not apply to the other functions.
times
New in version 1.1.0.
Specify smoothing factor \(\alpha\) directly, \(0 < \alpha \leq 1\).
Minimum number of observations in window required to have a value (otherwise result is NA).
Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average).
When adjust=True (default), the EW function is calculated using weights \(w_i = (1 - \alpha)^i\). For example, the EW moving average of the series [\(x_0, x_1, ..., x_t\)] would be:
adjust=True
When adjust=False, the exponentially weighted function is calculated recursively:
adjust=False
Ignore missing values when calculating weights; specify True to reproduce pre-0.15.0 behavior.
True
When ignore_na=False (default), weights are based on absolute positions. For example, the weights of \(x_0\) and \(x_2\) used in calculating the final weighted average of [\(x_0\), None, \(x_2\)] are \((1-\alpha)^2\) and \(1\) if adjust=True, and \((1-\alpha)^2\) and \(\alpha\) if adjust=False.
ignore_na=False
When ignore_na=True (reproducing pre-0.15.0 behavior), weights are based on relative positions. For example, the weights of \(x_0\) and \(x_2\) used in calculating the final weighted average of [\(x_0\), None, \(x_2\)] are \(1-\alpha\) and \(1\) if adjust=True, and \(1-\alpha\) and \(\alpha\) if adjust=False.
ignore_na=True
The axis to use. The value 0 identifies the rows, and 1 identifies the columns.
Times corresponding to the observations. Must be monotonically increasing and datetime64[ns] dtype.
datetime64[ns]
If str, the name of the column in the DataFrame representing the times.
If 1-D array like, a sequence with the same shape as the observations.
Only applicable to mean().
A Window sub-classed for the particular operation.
See also
rolling
Provides rolling window calculations.
expanding
Provides expanding transformations.
Notes
More details can be found at: Exponentially weighted windows.
Examples
>>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]}) >>> df B 0 0.0 1 1.0 2 2.0 3 NaN 4 4.0
>>> df.ewm(com=0.5).mean() B 0 0.000000 1 0.750000 2 1.615385 3 1.615385 4 3.670213
Specifying times with a timedelta halflife when computing mean.
>>> times = ['2020-01-01', '2020-01-03', '2020-01-10', '2020-01-15', '2020-01-17'] >>> df.ewm(halflife='4 days', times=pd.DatetimeIndex(times)).mean() B 0 0.000000 1 0.585786 2 1.523889 3 1.523889 4 3.233686