Pandas DataFrame.ewm: Exponentially Weighted Calculations on a DataFrame
Pandas DataFrame.ewm
The DataFrame.ewm
method in pandas is used to perform exponentially weighted calculations on a DataFrame. It allows you to apply operations (e.g., mean, variance, etc.) using exponential weighting, which gives more importance to recent observations.
Syntax
The syntax for DataFrame.ewm
is:
DataFrame.ewm(com=None, span=None, halflife=None, alpha=None, min_periods=0, adjust=True, ignore_na=False, axis=0, times=None, method='single')
Here, DataFrame
refers to the pandas DataFrame on which the exponentially weighted calculations are performed.
Parameters
Parameter | Description |
---|---|
com | Specifies the center of mass for the exponential weighting. Higher values give more weight to recent observations. |
span | Specifies the span of the exponential weighting. Higher values give more weight to recent observations. |
halflife | Specifies the half-life of the exponential weighting. Observations decay by half over this period. |
alpha | Specifies the smoothing factor for the exponential weighting. Must be between 0 and 1. |
min_periods | Specifies the minimum number of observations in the window required to have a value. Defaults to 0 . |
adjust | If True , weights are adjusted to account for the initial periods. Defaults to True . |
ignore_na | If True , missing values are ignored in the calculation. Defaults to False . |
axis | Specifies the axis along which the calculation is applied. Use 0 or 'index' for rows, and 1 or 'columns' for columns. Defaults to 0 . |
times | Specifies the time values for decay calculations when using halflife . |
method | Specifies the method for handling overlapping windows. Options include 'single' or 'table' . Defaults to 'single' . |
Returns
An ExponentialMovingWindow
object, which can be used to apply aggregation or transformation operations using exponential weighting.
Examples
Exponentially Weighted Mean Calculation on a DataFrame
This example demonstrates how to use ewm
to calculate the exponentially weighted mean of a DataFrame column.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'Values': [1, 2, 3, 4, 5]})
# Calculate the exponentially weighted mean
result = df['Values'].ewm(span=3).mean()
print(result)
Output
0 1.000000
1 1.750000
2 2.615385
3 3.550000
4 4.520661
Name: Values, dtype: float64
Exponentially Weighted Sum Calculation on a DataFrame
This example shows how to use ewm
to calculate the exponentially weighted sum of a DataFrame column.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'Values': [10, 20, 30, 40, 50]})
# Calculate the exponentially weighted sum
result = df['Values'].ewm(com=2).sum()
print(result)
Output
0 10.000000
1 30.000000
2 60.000000
3 100.000000
4 150.000000
Name: Values, dtype: float64
Exponentially Weighted Calculation with Halflife
This example demonstrates how to use ewm
with the halflife
parameter to calculate the exponentially weighted mean with a specified decay period.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'Values': [1, 2, 3, 4, 5]})
# Calculate the exponentially weighted mean with halflife=2
result = df['Values'].ewm(halflife=2).mean()
print(result)
Output
0 1.000000
1 1.666667
2 2.428571
3 3.266667
4 4.161290
Name: Values, dtype: float64
Exponentially Weighted Calculation with Ignored Missing Values
This example shows how to use ewm
with the ignore_na
parameter to calculate the exponentially weighted mean while ignoring missing values.
Python Program
import pandas as pd
# Create a DataFrame with missing values
df = pd.DataFrame({'Values': [1, None, 3, 4, 5]})
# Calculate the exponentially weighted mean while ignoring missing values
result = df['Values'].ewm(span=3, ignore_na=True).mean()
print(result)
Output
0 1.000000
1 1.000000
2 2.615385
3 3.550000
4 4.520661
Name: Values, dtype: float64
Summary
In this tutorial, we explored the DataFrame.ewm
method in pandas. Key takeaways include:
- Using
ewm
to perform exponentially weighted calculations (e.g., mean, sum) on a DataFrame. - Controlling the weighting with parameters like
com
,span
,halflife
, andalpha
. - Ignoring missing values with the
ignore_na
parameter. - Understanding the flexibility of
ewm
for time-series analysis and smoothing.