Skip to main content

Time-based feature engineering

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.

Time-based feature engineering checkpoint
Which time-aware feature would add the most value to one of your datasets: lag, rolling statistics, seasonality, decomposition or event encoding?
Your reflection here...
Would you favour batch-created historical features, real-time event features or a combination of both?
Your reflection here...