Datetime & Cyclical Features
Transforming raw timestamps into calendar features and continuous sine cosine cyclical signals for machine learning models.
The Raw Timestamp Problem
Raw timestamps (for example 2026-08-08 14:32:00 or Unix Epoch 1786199520) are difficult for machine learning models to use directly:
- Non-Stationary: Epoch seconds increase continuously into the future, causing out-of-distribution issues for tree models and linear layers.
- Unstructured: Raw timestamps hide rich calendar patterns like day of week, rush hour times, or holiday effects.
To make time useful, we extract Calendar Component Features and Cyclical Trigonometric Features.
Raw Timestamp ──► Extract Calendar Components ──► Transform via Sine/Cosine ──► Machine Learning Inputs
1. Extracting Calendar Component Features
Deconstruct raw timestamps into categorical and ordinal features:
- Time of Day:
Hour($0\text{--}23$),Minute($0\text{--}59$). - Calendar Period:
Day_of_Week($0\text{--}6$),Day_of_Month($1\text{--}31$),Month($1\text{--}12$). - Business Flags:
Is_Weekend($0/1$),Is_Holiday($0/1$),Is_Business_Hours($0/1$).
These discrete categories allow tree models to split on specific temporal rules (for example "If Is_Weekend == 1 AND Hour >= 18").
2. Cyclical Sine / Cosine Transformations
Consider Hour_of_Day represented as a raw integer from $0$ to $23$:
- Hour $23$ ($11\text{ PM}$) and Hour $0$ ($12\text{ AM}$) are only 1 hour apart in reality.
- A neural network or distance algorithm sees $23$ and $0$ as being 23 units apart!
To preserve continuous periodic distance across midnight, transform the variable using Sine and Cosine pairs:
$$x_{\sin} = \sin\left(\frac{2 \pi \cdot t}{T}\right), \quad x_{\cos} = \cos\left(\frac{2 \pi \cdot t}{T}\right)$$
Where $t$ is the current time value (Hour $23$) and $T$ is the total period length ($24$ hours).
HOUR OF DAY ON 2D SINE/COSINE CIRCLE
Hour 0 (12 AM)
(sin=0, cos=1)
▲
┌─────────────┴─────────────┐
│ • │
Hour 18 (6 PM) ───┤ • • ├─── Hour 6 (6 AM)
(sin=-1, cos=0) │ │ (sin=1, cos=0)
│ • │
└─────────────┬─────────────┘
▼
Hour 12 (12 PM)
(sin=0, cos=-1)
Mapping time onto a 2D unit circle ensures that Hour $23$ and Hour $0$ sit right next to each other in Euclidean space!
Say this out loud
Datetime features transform raw timestamps into informative model inputs. Extracting calendar components like day of week and holiday flags captures structural demand shifts. Converting periodic variables like hour of day into sine and cosine pairs preserves continuous distance across cycle boundaries like midnight.
Followups to expect
- Why must you supply BOTH sine and cosine features for a cyclical variable? A single sine feature outputs the exact same value twice in a single 24-hour cycle (for example at 6 AM and 6 PM). Adding cosine breaks the ambiguity, giving every time point a unique 2D position on the unit circle.
- What is Time-Lagged Feature Engineering? Creating rolling historical aggregation features (for example average user clicks over the last 1 hour, 24 hours, or 7 days) to capture temporal momentum.
Check yourself
Why does representing 'Hour of Day' as a raw integer from 0 to 23 create a artificial continuity breakdown in distance models?