What is feature engineering for time series (lags, rolling stats, datetime features)?
sobes.tech AI
Answer from AI
Feature engineering for time series is the process of creating new features from raw data that help models better capture temporal structure and dependencies.
-
Lags — creating features that represent the values of the time series from previous time points. For example, the value of the series 1, 2, 3 periods ago. This helps the model account for autocorrelation.
-
Rolling stats — computing statistics (mean, median, standard deviation, etc.) over a rolling window. For example, the average over the last 7 days. This helps identify trends and smooth out noise.
-
Datetime features — extracting features from timestamps such as day of the week, month, hour, holidays, seasonality, etc. This helps the model consider seasonality and cyclicality.
An example of creating lags and rolling means in Python with pandas:
import pandas as pd
df = pd.DataFrame({'value': [10, 12, 13, 15, 14, 16, 18]})
# Lag of 1 period
df['lag_1'] = df['value'].shift(1)
# Rolling mean over 3 periods
df['rolling_mean_3'] = df['value'].rolling(window=3).mean()
Thus, feature engineering helps time series models better understand data structure and improve forecast quality.