To calculate exponential moving averages in pandas, we can use the pandas ewm() function.

df.ewm(span=10, adjust=False).mean() # Calculate the Exponential Weighted Moving Average over a span of 10 periods

When working with data, many times we want to calculate summary statistics to understand our data better. One such statistic is the moving average of time series data. With pandas, we can calculate both equal weighted moving averages and exponential weighted moving averages.

To calculate exponential weights moving averages in Python, we can use the pandas ewm() function.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Month': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], 
      'Weight':[100,110,120,115,105,120,125,100,120,110,105,130,120,135,125,115,120,110,100,115]})

print(df)
# Output: 
    Month  Weight
0       1     100
1       2     110
2       3     120
3       4     115
4       5     105
5       6     120
6       7     125
7       8     100
8       9     120
9      10     110
10     11     105
11     12     130
12     13     120
13     14     135
14     15     125
15     16     115
16     17     120
17     18     110
18     19     100
19     20     115

Let’s calculate some exponentially weighted moving averages with the pandas ewm() function.

First, let’s start with a moving average with each average spanning 5 periods.

df["5 Period EMA"] = df["Weight"].ewm(span=5).mean()
print(df)

# Output:
    Month  Weight  5 Period EMA
0       1     100    100.000000
1       2     110    106.000000
2       3     120    112.631579
3       4     115    113.615385
4       5     105    110.308057
5       6     120    113.849624
6       7     125    117.797475
7       8     100    111.624108
8       9     120    114.490637
9      10     110    112.967342
10     11     105    110.280498
11     12     130    116.904721
12     13     120    117.941809
13     14     135    123.647417
14     15     125    124.099310
15     16     115    121.061582
16     17     120    120.707362
17     18     110    117.135825
18     19     100    111.421305
19     20     115    112.614562

If we want a bigger window, we can adjust the “span” parameter.

df["8 Period EMA"] = df["Weight"].ewm(span=8).mean()
print(df)

# Output:
   Month  Weight  8 Period EMA
0       1     100    100.000000
1       2     110    105.625000
2       3     120    111.658031
3       4     115    112.829327
4       5     105    110.397235
5       6     120    113.137905
6       7     125    116.322207
7       8     100    112.134192
8       9     120    114.085385
9      10     110    113.097489
10     11     105    111.177039
11     12     130    115.575478
12     13     120    116.597668
13     14     135    120.812017
14     15     125    121.764646
15     16     115    120.233939
16     17     120    120.181217
17     18     110    117.893909
18     19     100    113.883645
19     20     115    114.133363

Other Exponential Weighted Functions with pandas ewm()

With the pandas ewm() function, we can calculate more than just exponentially weighted moving averages.

We can also calculate exponentially weighted variances, standard deviations, correlations, and covariances.

To do this, we just need to tack on the appropriate function call.

For example, if we have the same dataset from above, we can calculate the exponentially weighted variance by using the pandas var() function.

df["5 Period EW Var"] = df["Weight"].ewm(span=5).var()
print(df)

# Output:
    Month  Weight  5 Period EW Var
0       1     100              NaN
1       2     110        50.000000
2       3     120        97.368421
3       4     115        53.188259
4       5     105        54.839592
5       6     120        62.391316
6       7     125        76.276032
7       8     100       140.828706
8       9     120       111.987911
9      10     110        79.362125
10     11     105        70.280399
11     12     130       155.434896
12     13     120       105.895898
13     14     135       151.521081
14     15     125       101.349800
15     16     115        90.523566
16     17     120        60.616397
17     18     110        72.258521
18     19     100       129.758539
19     20     115        90.044759

If we want to calculate an exponentially weighted moving standard deviation, we can use the pandas std() function.

df["5 Period EW Std"] = df["Weight"].ewm(span=5).std()
print(df)

# Output:
    Month  Weight  5 Period EW Std
0       1     100              NaN
1       2     110         7.071068
2       3     120         9.867544
3       4     115         7.293028
4       5     105         7.405376
5       6     120         7.898817
6       7     125         8.733615
7       8     100        11.867127
8       9     120        10.582434
9      10     110         8.908542
10     11     105         8.383341
11     12     130        12.467353
12     13     120        10.290573
13     14     135        12.309390
14     15     125        10.067264
15     16     115         9.514387
16     17     120         7.785653
17     18     110         8.500501
18     19     100        11.391161
19     20     115         9.489192

Hopefully this article has been helpful for you to understand how to use the pandas ewm() function to calculate exponentially weighted moving averages and other statistics in your Python code.

Categorized in:

Python,

Last Update: March 20, 2024