← Lessons

quiz vs the machine

Gold1340

Machine Learning

Datetime Feature Extraction

Decompose timestamps into parts and cyclical encodings that reveal temporal patterns.

5 min read · core · beat Gold to climb

Datetime Feature Extraction

A raw timestamp is nearly useless to most models, but it hides rich structure. Extracting calendar parts and cyclical encodings exposes seasonality and routine.

Useful extracted parts

  • Calendar fields such as year, month, day, hour, and day of week.
  • Flags like is weekend, is holiday, or is business hours.
  • Elapsed time since a reference event, such as days since signup.

These let a model learn that sales spike on weekends or that traffic peaks at certain hours.

Cyclical encoding

Hour twenty three and hour zero are adjacent in time but far apart as numbers. Encoding a cyclic field with its sine and cosine places the values on a circle, so the model sees that late night and early morning are close.

Cautions

  • Beware of leakage from future timestamps that would not be known at prediction time.
  • Time zones and daylight saving shifts can silently corrupt extracted hours.

Key idea

Datetime extraction unpacks timestamps into calendar parts, flags, and cyclical sine cosine encodings, surfacing seasonality while guarding against leakage and time zone errors.

Check yourself

Answer to earn rating on the learn ladder.

1. Why encode the hour with sine and cosine?

2. What silent error can corrupt extracted hours?