augment_fourier

augment_fourier(data, date_column, periods=1, max_order=1, reduce_memory=True, engine='pandas')

Adds Fourier transforms to a Pandas DataFrame or DataFrameGroupBy object.

The augment_fourier function takes a Pandas DataFrame or GroupBy object, a date column, a value column or list of value columns, the number of periods for the Fourier series, and the maximum Fourier order, and adds Fourier-transformed columns to the DataFrame.

Parameters

Name Type Description Default
data pd.DataFrame or pd.core.groupby.generic.DataFrameGroupBy The data parameter is the input DataFrame or DataFrameGroupBy object that you want to add Fourier-transformed columns to. required
date_column str The date_column parameter is a string that specifies the name of the column in the DataFrame that contains the dates. This column will be used to compute the Fourier transforms. required
periods int or list The periods parameter specifies how many timesteps between each peak in the fourier series. Default is 1. 1
max_order int The max_order parameter specifies the maximum Fourier order to calculate. Default is 1. 1
reduce_memory bool The reduce_memory parameter is used to specify whether to reduce the memory usage of the DataFrame by converting int, float to smaller bytes and str to categorical data. This reduces memory for large data but may impact resolution of float and will change str to categorical. Default is False. True
engine str The engine parameter is used to specify the engine to use for augmenting lags. It can be either “pandas” or “polars”. - The default value is “pandas”. - When “polars”, the function will internally use the polars library. This can be faster than using “pandas” for large datasets. 'pandas'

Returns

Type Description
pd.DataFrame A Pandas DataFrame with Fourier-transformed columns added to it.

Examples

import pandas as pd
import pytimetk as tk

df = tk.load_dataset('m4_daily', parse_dates=['date'])

# Example 1 - Add Fourier transforms for a single column
fourier_df = (
    df
        .query("id == 'D10'")
        .augment_fourier(
            date_column='date',
            periods=[1, 7],
            max_order=1
        )
)
fourier_df.head()

fourier_df.plot_timeseries("date", "date_sin_1_7", x_axis_date_labels = "%B %d, %Y",)
# Example 2 - Add Fourier transforms for grouped data
fourier_df = (
    df
        .groupby("id")
        .augment_fourier(
            date_column='date',
            periods=[1, 7],
            max_order=1,
            engine= "pandas"
        )
)
fourier_df
id date value date_sin_1_1 date_sin_1_7 date_cos_1_1 date_cos_1_7
0 D10 2014-07-03 2076.199951 0.394510 0.057901 -0.918892 -0.998322
1 D10 2014-07-04 2073.399902 -0.980653 0.645246 -0.195753 -0.763975
2 D10 2014-07-05 2048.699951 0.011484 0.974562 0.999934 -0.224120
3 D10 2014-07-06 2048.899902 0.975899 0.914158 -0.218224 0.405359
4 D10 2014-07-07 2006.400024 -0.415510 0.488189 -0.909589 0.872738
... ... ... ... ... ... ... ...
9738 D500 2012-09-19 9418.799805 -0.953668 0.999047 0.300860 -0.043643
9739 D500 2012-09-20 9365.700195 0.491755 0.825474 0.870734 0.564440
9740 D500 2012-09-21 9445.900391 0.750080 0.321800 -0.661347 0.946808
9741 D500 2012-09-22 9497.900391 -0.802291 -0.310559 -0.596933 0.950554
9742 D500 2012-09-23 9545.299805 -0.417929 -0.818728 0.908480 0.574181

9743 rows × 7 columns

# Example 3 - Add Fourier transforms for grouped data
fourier_df = (
    df
        .groupby("id")
        .augment_fourier(
            date_column='date',
            periods=[1, 7],
            max_order=1,
            engine= "polars"
        )
)
fourier_df
id date value date_sin_1_1 date_sin_1_7 date_cos_1_1 date_cos_1_7
0 D10 2014-07-03 2076.199951 0.394510 0.057901 -0.918892 -0.998322
1 D10 2014-07-04 2073.399902 -0.980653 0.645246 -0.195753 -0.763975
2 D10 2014-07-05 2048.699951 0.011484 0.974562 0.999934 -0.224120
3 D10 2014-07-06 2048.899902 0.975899 0.914158 -0.218224 0.405359
4 D10 2014-07-07 2006.400024 -0.415510 0.488189 -0.909589 0.872738
... ... ... ... ... ... ... ...
9738 D500 2012-09-19 9418.799805 -0.953668 0.999047 0.300860 -0.043643
9739 D500 2012-09-20 9365.700195 0.491755 0.825474 0.870734 0.564440
9740 D500 2012-09-21 9445.900391 0.750080 0.321800 -0.661347 0.946808
9741 D500 2012-09-22 9497.900391 -0.802291 -0.310559 -0.596933 0.950554
9742 D500 2012-09-23 9545.299805 -0.417929 -0.818728 0.908480 0.574181

9743 rows × 7 columns