Feature Engineering Tricks for Time Series Data

Time series forecasting is a critical component of many business applications, from inventory management to financial trading. While choosing the right model is important, feature engineering often yields the biggest improvements.

In this article, I'll share some practical feature engineering tricks that I've used to boost model performance in real-world projects.

Advertisement (In-Content)

1. Time-Based Features

The simplest features to extract are those derived directly from the timestamp. These help models capture seasonality and cyclic patterns.

  • Hour of Day: Crucial for hourly data (e.g., electricity demand).
  • Day of Week: Captures weekly cycles (e.g., higher sales on weekends).
  • Month of Year: Captures annual seasonality.
  • Is Weekend / Is Holiday: Boolean flags that often have strong predictive power.
df['hour'] = df.index.hour
df['dayofweek'] = df.index.dayofweek
df['month'] = df.index.month
df['is_weekend'] = df.index.dayofweek >= 5

2. Lag Features

Lag features allow the model to look specifically at past values. For instance, the sales value from exactly one week ago is often a strong predictor of sales today.

Tip: Be careful with data leakage! Ensure you're not using future data to predict the past.

Experiment with different lag windows (e.g., lag_1, lag_7, lag_30) to see what works best for your specific temporal dynamics.

3. Rolling Window Statistics

Rolling means, standard deviations, and max/min values over a window (e.g., last 7 days) smooth out noise and capture trends.

df['rolling_mean_7d'] = df['value'].rolling(window=7).mean()
df['rolling_std_7d'] = df['value'].rolling(window=7).std()
Advertisement (In-Content)

Conclusion

Feature engineering is an art as much as a science. By creatively extracting signals from time stamps and past values, you can significantly outperform complex models trained on raw data alone.