Course contentsShow
← Machine Learning and Deep Learning
Lesson 442 of 3,53810. Feature EngineeringFree lesson

Time-Based Feature Engineering

Extract temporal features like day of week, seasonality, and time since event from datetime data.

Time-Based Feature Engineering

What you'll learn: How to unlock hidden patterns in datetime data by extracting meaningful temporal features that algorithms can actually use.

Why Time Needs Special Treatment

Raw datetime values like "2024-03-15 14:30:00" are nearly useless to most machine learning algorithms. They're just strings or numbers that don't capture the rich patterns hidden in time—like whether sales spike on weekends, or if customer behavior changes seasonally.

Core Idea: Breaking Time Into Components

Time-based feature engineering transforms datetime data into multiple informative features. Think of it like taking apart a clock to understand its pieces: instead of just seeing "3 PM," you see the hour, the day type, and how it relates to other events.

Common Temporal Features

Cyclical patterns:

  • Day of week (Monday = different shopping behavior than Saturday)
  • Month (seasonality in retail, agriculture, energy use)
  • Hour of day (traffic patterns, website activity)
  • Quarter (business cycles)

Event-relative features:

  • Time since last purchase (customer recency)
  • Days until holiday (anticipatory behavior)
  • Time since account creation (user tenure)

The Real-World Analogy

Imagine predicting restaurant occupancy. Knowing it's "January 15, 2024, 7:30 PM" as a raw number tells the model nothing. But extracting:

  • Day = Monday (slower weeknight)
  • Hour = 7 PM (dinner rush)
  • Days since New Year = 15 (resolution diets fading)

...gives your model actionable patterns it can learn from.

Capturing Cycles

For truly cyclical features like hour or month, you might need to handle the "wrap-around" (December and January are neighbors, but 12 and 1 look far apart numerically)—though we'll explore advanced techniques for this later.

Key Takeaway: Time-based feature engineering transforms unusable datetime stamps into learnable patterns by extracting components like day of week, seasonality indicators, and time elapsed since important events.