Time Series ForecastingFeature Engineering (Lag Features, Rolling Stats, Seasonality)Medium⏱️ ~2 min

Seasonality Encoding: Calendar Features and Fourier Terms

Seasonality captures repeating patterns at fixed intervals, such as hourly traffic peaks, weekly shopping cycles, or yearly holiday demand. Encoding seasonality transforms these periodic structures into features that models can consume. The two main approaches are calendar features and Fourier terms, each suited to different periodicities and model types. Calendar features extract components like hour of day, day of week, day of month, or month of year from timestamps. For a tree based model, these can be integer encodings (Monday = 1, Tuesday = 2). For linear models, use one hot encoding to avoid imposing false ordinality. Netflix recommendation systems include hour of day (0 to 23) and day of week (0 to 6) as features, capturing the fact that users watch different content on Friday night versus Tuesday morning. Adding holiday indicators (binary flags for Thanksgiving, Christmas, New Year) further improves accuracy during high traffic periods. Fourier terms encode cyclic patterns as sine and cosine pairs, which naturally represent periodicity without creating dozens of dummy variables. For a weekly pattern with period P equals 7, the first harmonic is sin(2 * pi * t / 7) and cos(2 * pi * t / 7). Adding a second harmonic doubles the frequency: sin(4 * pi * t / 7) and cos(4 * pi * t / 7). Two to four harmonics typically capture smooth seasonal curves well. Uber's ETA model uses two Fourier harmonics for weekly traffic patterns and one harmonic for daily patterns, requiring just 6 features total instead of 24 hour dummies plus 7 day dummies. The tradeoff is expressiveness versus dimensionality. Calendar features are interpretable and work well when seasonality aligns with calendar boundaries, but explode in size for long periods (52 weeks per year means 52 dummies). Fourier terms are compact and smooth, ideal for long smooth cycles like yearly trends, but less effective for abrupt transitions like Black Friday spikes. Hybrid approaches use calendar features for short strong periodicities (day of week) and Fourier terms for long smooth ones (yearly). Multiple seasonalities interact. Monday 9 AM demand differs from Saturday 9 AM. Simple additive features miss this. Solutions include interaction features (hour times day of week), separate models per segment, or Fourier terms at multiple periods. For retail, including both daily and weekly Fourier harmonics captures these cross effects with fewer features than full interaction matrices.
💡 Key Takeaways
Calendar features extract hour of day, day of week, and holiday flags. Netflix uses hour (0 to 23) and day of week (0 to 6) to capture viewing pattern shifts, with holiday indicators for major traffic spikes
Fourier terms encode periodicity as sine and cosine pairs. Two to four harmonics for weekly or yearly cycles provide smooth approximations with far fewer features than one hot encoding (6 features vs 52 week dummies)
Uber ETA uses two weekly Fourier harmonics and one daily harmonic (6 features) to capture traffic seasonality instead of 24 hour plus 7 day dummy variables (31 features), reducing model size and overfitting
Short strong periodicities like day of week work well as calendar features for interpretability. Long smooth cycles like yearly trends are better as Fourier terms to avoid dimensionality explosion
Multiple seasonalities interact. Monday 9 AM differs from Saturday 9 AM. Hybrid approaches combine calendar features for short periods with Fourier terms for long periods, or include interaction terms like hour times day of week
Abrupt seasonality changes like Black Friday spikes are hard to capture with smooth Fourier terms. Use explicit event indicators or separate models for special periods when transitions are sharp rather than gradual
📌 Examples
Amazon retail demand forecasting uses day of week (7 categories), week of month (4 categories), and holiday binary flags plus 3 yearly Fourier harmonics, totaling 14 seasonality features that improve Mean Absolute Percentage Error (MAPE) by 8 percent over non seasonal baselines
Airbnb pricing model encodes check in day of week, days until check in, and 4 Fourier terms for yearly seasonality, capturing summer vacation peaks and winter dips with 11 features, trained on 200 million booking events
DoorDash delivery time prediction uses hour of day (0 to 23), day of week (0 to 6), and binary indicators for lunch (11 AM to 1 PM) and dinner (5 PM to 8 PM) rush periods, improving p95 ETA error from 12 minutes to 8 minutes
← Back to Feature Engineering (Lag Features, Rolling Stats, Seasonality) Overview
Seasonality Encoding: Calendar Features and Fourier Terms | Feature Engineering (Lag Features, Rolling Stats, Seasonality) - System Overflow