Skip to main content
Instruction and application
In Progress

Time-based feature engineering

Time is more than a column in a dataset. It is often the structure that explains recency, momentum, seasonality and behavioural change.

Microscope illustration

Lag features

Lag features give a model memory by shifting earlier values forward in time. They are especially useful when the immediate past is predictive of what happens next.

  • demand_lag_1 could mean yesterday's demand
  • demand_lag_7 could mean the value from the same day last week
import pandas as pd

df["demand_lag_1"] = df["demand"].shift(1)
df["demand_lag_7"] = df["demand"].shift(7)

Rolling window statistics

Rolling features summarise recent behaviour rather than a single earlier value.

  • Rolling mean captures recent level
  • Rolling standard deviation captures recent volatility
df["rolling_mean_7"] = df["tickets"].rolling(window=7).mean()
df["rolling_std_14"] = df["tickets"].rolling(window=14).std()

Seasonality extraction

Many processes follow daily, weekly or yearly cycles. Features such as day_of_week, month, is_weekend or cyclical sine/cosine encodings help the model recognise those recurring patterns directly.

Time-based decomposition

Decomposition separates a time series into trend, seasonality and residual components. This can make it easier to model long-term movement and isolate unusual events.

Event encoding

Real-world systems change because things happen: a user logs in, a promotion launches, a machine is serviced or a product price changes. Event-based features such as days_since_last_login or is_during_promo help capture those causal shifts.

Lightbulb icon

Why this matters

Time-aware features often outperform raw timestamps because they express what matters operationally: recency, change, momentum, cycles and event timing.

You're building a model to predict daily electricity usage. A stakeholder asks how the model will account for repeating patterns like higher demand on weekdays. Which feature would best support this?

You're working on an anomaly detection model for factory sensor data. You want to help the model detect sudden fluctuations. Which feature would best support this goal?

You're designing a customer churn model for a streaming platform. Which of the following features would best capture recent user inactivity?

You're forecasting sales for a subscription product. You've already added lag and rolling features. Which of the following would best help the model distinguish between growth trends, seasonal effects and noise?

In progress