Time-based feature engineering
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.

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_1could mean yesterday's demanddemand_lag_7could 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.
Why this matters
Time-aware features often outperform raw timestamps because they express what matters operationally: recency, change, momentum, cycles and event timing.