import pandas as pd
import polars as pl
import pytimetk as tk
= tk.load_dataset("m4_daily", parse_dates = ['date'])
df
# Pandas example (engine inferred)
= (
ewm_df
df'id')
.groupby(
.augment_ewm(= 'date',
date_column = 'value',
value_column = [
window_func 'mean',
'std',
],= 0.1,
alpha
)
)
# Polars example using the tk accessor
= (
ewm_pl
pl.from_pandas(df)'id')
.group_by(
.tk.augment_ewm(='date',
date_column='value',
value_column='mean',
window_func=0.1,
alpha
) )
augment_ewm
augment_ewm(
data,
date_column,
value_column,='mean',
window_func=None,
alpha=False,
reduce_memory='auto',
engine**kwargs,
)
Add Exponential Weighted Moving (EWM) window functions to a DataFrame or GroupBy object.
The augment_ewm
function applies Exponential Weighted Moving (EWM) window functions to specified value columns of a DataFrame and adds the results as new columns.
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | DataFrame or GroupBy(pandas or polars) | The input data to augment. Grouped inputs are processed per group before the EWM columns are appended. | required |
date_column | str | The name of the column containing date information in the input DataFrame or GroupBy object. | required |
value_column | Union[str, list] | The value_column parameter is used to specify the column(s) on which the Exponential Weighted Moving (EWM) calculations will be performed. It can be either a string or a list of strings, representing the name(s) of the column(s) in the input DataFrame or GroupBy |
required |
window_func | Union[str, list] | The window_func parameter is used to specify the Exponential Weighted Moving (EWM) window function(s) to apply. It can be a string or a list of strings. The possible values are: - ‘mean’: Calculate the exponentially weighted mean. - ‘median’: Calculate the exponentially weighted median. - ‘std’: Calculate the exponentially weighted standard deviation. - ‘var’: Calculate the exponentially weighted variance. |
'mean' |
alpha | float | The alpha parameter is a float that represents the smoothing factor for the Exponential Weighted Moving (EWM) window function. It controls the rate at which the weights decrease exponentially as the data points move further away from the current point. |
None |
engine | (auto, pandas, polars, cudf) | Execution engine. "auto" (default) infers the backend from the input data while allowing explicit overrides. Polars and cudf inputs currently execute through a pandas fallback. |
"auto" |
reduce_memory | bool | Attempt to reduce memory usage before/after computation when operating on pandas data. If a polars input is supplied a warning is emitted and no conversion occurs. | False |
**kwargs | Additional arguments that are directly passed to the pandas EWM method. For more details, refer to the “Notes” section below. | {} |
Returns
Name | Type | Description |
---|---|---|
DataFrame | The function augment_ewm returns a DataFrame augmented with the results of the Exponential Weighted Moving (EWM) calculations. |
Notes
Any additional arguments provided through **kwargs are directly passed to the pandas EWM method. These arguments can include parameters like ‘com’, ‘span’, ‘halflife’, ‘ignore_na’, ‘adjust’ and more.
For a comprehensive list and detailed description of these parameters:
Refer to the official pandas documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.ewm.html
Or, within an interactive Python environment, use:
?pandas.DataFrame.ewm
to display the method’s docstring.