Data & Feature Engineering

Datetime & Cyclical Features

Transforming raw timestamps into calendar features and continuous sine cosine cyclical signals for machine learning models.

🟢 beginner5 min readfeatures
Datetime and Cyclical Features transform raw timestamp values into informative signals for machine learning models. Raw timestamps (like Unix epoch seconds) are non stationary and difficult for neural networks to interpret. Feature engineering extracts calendar components (Hour of Day, Day of Week, Is Weekend) and uses Sine Cosine Trigonometric Transformations to capture smooth continuous cyclical time patterns.

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:

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:

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$:

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

  1. 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.
  2. 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

Question 1 of 3

Why does representing 'Hour of Day' as a raw integer from 0 to 23 create a artificial continuity breakdown in distance models?

More in Data & Feature Engineering

See all →
Feature Engineering Fundamentals4 minSQL Questions in ML Interviews5 minEncoding Categorical Variables4 min