Pandas ewm()

The ewm() method in Pandas provides Exponential Weighted functions, which are useful for smoothing data and emphasizing more on recent observations.

An exponentially weighted function applies more weight to more recent observations, making it particularly useful in time series analysis or financial data analysis.

Pandas ewm() doesn't return any meaningful output on its own, so the aggregate function like mean() is used in conjunction with ewm().

Example

import pandas as pd

# sample DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}

df = pd.DataFrame(data)

# apply exponential weighting
ewm_df = df.ewm(alpha=0.5).mean()

print(ewm_df)

'''
Output

          A         B
0  1.000000  5.000000
1  1.666667  5.666667
2  2.428571  6.428571
3  3.266667  7.266667
'''

ewm() Syntax

The syntax of the ewm() method in Pandas is:

df.ewm(com=None, span=None, halflife=None, alpha=None, min_periods=0, adjust=True, ignore_na=False, axis=0, times=None, method='single')

ewm() Arguments

The ewm() method has the following arguments:

  • com (optional): specifies decay in terms of center of mass
  • span (optional): specifies decay in terms of span
  • halflife (optional): specifies decay in terms of half-life
  • alpha (optional): specifies smoothing factor directly
  • min_periods (optional): minimum number of observations in a window required to have a value
  • adjust (optional): whether to divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings
  • ignore_na (optional): whether to ignore missing values when calculating weights.
  • axis (optional): the axis to apply the weights
  • times (optional): specifies time points for each data point
  • method (optional): determines how the weights are applied when calculating the exponential moving average

ewm() Return Value

The ewm() method returns an EWM object that can then be used to apply various methods, such as mean, var, std, etc., to get the corresponding exponentially weighted statistics.


Example 1: Simple Exponential Smoothing

import pandas as pd

# sample DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}

df = pd.DataFrame(data)

# apply exponential weighting
ewm_df = df.ewm(span=2).mean()

print(ewm_df)

Output

          A         B
0  1.000000  5.000000
1  1.750000  5.750000
2  2.615385  6.615385
3  3.550000  7.550000

In this example, we calculated the exponentially weighted moving average with window size 2 using the span=2 argument.

Here, the span argument specifies the window size for calculating the exponentially weighted average. A smaller span means more weight is given to recent values.

Note: The mean() method calculates the exponentially weighted moving average. Other aggregate functions can be used to calculate other exponentially weighted statistics similarly.


Example 2: Exponential Smoothing with Adjusted and Minimum Periods

import pandas as pd

# sample DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}

df = pd.DataFrame(data)

# apply exponential weighting
ewm_df = df.ewm(alpha=0.3, adjust=False, min_periods=3).mean()

print(ewm_df)

Output

       A        B
0    NaN    NaN
1    NaN    NaN
2   1.810   5.810
3   2.467   6.467

Here,

  • alpha=0.3: sets the smoothing factor directly. A higher alpha gives more weight to recent observations
  • adjust=False: implies that the weights are applied to the observations as is, without any bias adjustment
  • min_periods=3: means that the exponential moving average will only be calculated for windows with at least 3 observations.

Example 3: Exponential Smoothing with Different Alpha Values

import pandas as pd

# sample DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}

df = pd.DataFrame(data)

# apply exponential weighting
ewm_df = df.ewm(alpha=0.1).mean()

print(ewm_df)

Output

          A         B
0  1.000000  5.000000
1  1.526316  5.526316
2  2.070111  6.070111
3  2.631288  6.631288

Here, alpha=0.1 applies the exponential weighting with an alpha of 0.1.

A smaller alpha implies less weight to recent observations, resulting in smoother output but less responsive to recent changes.


Example 4: Exponential Smoothing with Half Life

import pandas as pd

# sample DataFrame
data = {'A': [1, 2, 3, 4],
        'B': [5, 6, 7, 8]}

df = pd.DataFrame(data)

# apply exponential weighting
ewm_df = df.ewm(halflife=2).mean()

print(ewm_df)

Output

0  1.000000  5.000000
1  1.585786  5.585786
2  2.226541  6.226541
3  2.919120  6.919120

Here, the halflife of 2 means that the weights reduce by half every two periods, significantly impacting the smoothed values.


Example 5: Exponential Smoothing with Ignore NA and Adjust

import pandas as pd

# sample DataFrame
data = {'A': [None, 2, None, 4, 5],
        'B': [1, None, 3, None, 5]}

df = pd.DataFrame(data)

# apply exponential weighting
ewm_df = df.ewm(alpha=0.2, adjust=True, ignore_na=True).mean()

print(ewm_df)

Output

          A         B
0       NaN  1.000000
1  2.000000  1.000000
2  2.000000  2.111111
3  3.111111  2.111111
4  3.885246  3.295082

Here,

  • ignore_na=True: allows the method to skip NaN values without raising an error
  • adjust=True: provides a more balanced weighting of the observations