Contents
Figure 1: Stop One-Hot Encoding Hours and Days — your model thinks 11 PM and midnight sit 23 hours apart, when they sit one hour apart
Your model thinks 11 PM and midnight sit 23 hours apart. They sit one hour apart. That mismatch quietly wrecks time-based features, and plenty of people patch it by one-hot encoding every hour, which creates 24 columns to say what two columns could say.
This guide explains cyclical sine/cosine encoding, shows when it beats one-hot encoding, and tells you when it does not. I ran every experiment below on a fixed synthetic generator, so you can reproduce the numbers, and the results surprised me. The title is a provocation, because the honest answer is "it depends."
The Problem With Raw Hours and Days
Hours, weekdays and months repeat in a circle. Hour 23 is followed by hour 0, and Sunday is followed by Monday. If you feed the raw number into a model, the model sees a line, and the wrap-around breaks.
| Pair | Raw difference | Distance once encoded |
|---|---|---|
| 23:00 and 00:00 | 23 | 0.261 |
| 00:00 and 01:00 | 1 | 0.261 |
| 00:00 and 12:00 | 12 | 2.000 |
With raw hours, the distance between 23 and 0 equals 23, while the distance between 0 and 1 equals 1. In reality both pairs sit exactly one hour apart.
The model also treats time as a straight ramp. A linear model with a raw hour column can only say "demand rises as the hour rises." Real demand curves rise, peak, fall and loop back.
Raw Hours Are Worse Than a Ramp: They Are Phase-Dependent
Here is a finding I did not expect. I took one smooth daily wave and shifted it around the clock, changing nothing else. Raw hour's R² moved from 0.008 to 0.53.
| Peak of the same wave | Raw hour R² |
|---|---|
| 06:00 | 0.530 |
| 12:00 | 0.008 |
| 18:00 | 0.525 |
| 00:00 | 0.010 |
That is not signal. It is an artifact of where the trough falls on the 0–23 axis. A raw hour column accidentally captures whatever part of the cycle looks linear, so its score depends on the phase of your data rather than on whether the feature is sound. You cannot know which one you got without testing, which is exactly why the encoding should not be left to chance.
What Cyclical Encoding Does
Cyclical encoding maps each value onto a circle. You convert the value to an angle, then use its sine and cosine as two features. Two columns together give every hour its own point on the circle.
- Angle:
2 * pi * value / period - Feature 1:
sin(angle) - Feature 2:
cos(angle)
The period is the length of the cycle: 24 for hours, 7 for weekdays, 12 for months and about 365.25 for day of year. After encoding, hour 23 and hour 0 sit 0.261 apart, exactly the same distance as hour 0 and hour 1. Hour 0 and hour 12 sit 2.0 apart, the diameter of the circle.
Why do you need both functions? Sine alone gives the same value at two different times, like 3 AM and 9 AM. Cosine breaks the tie. Together they identify each point on the circle uniquely.
The Code
Here is a reusable transformer that plugs into a scikit-learn pipeline. I ran this snippet, and it produces the expected shapes.
import numpy as np
from sklearn.preprocessing import FunctionTransformer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import Ridge
def cyclical(X, period, harmonics=1):
X = np.asarray(X, dtype=float)
angle = 2 * np.pi * X / period
cols = []
for k in range(1, harmonics + 1):
cols += [np.sin(k * angle), np.cos(k * angle)]
return np.hstack(cols)
prep = ColumnTransformer([
('hour', FunctionTransformer(cyclical, kw_args={'period': 24}), ['hour']),
('dow', FunctionTransformer(cyclical, kw_args={'period': 7}), ['dow']),
], remainder='passthrough')
model = Pipeline([('prep', prep), ('reg', Ridge())])
The harmonics argument adds extra sine and cosine pairs at higher frequencies, and you will see why that matters in a moment. To turn the features back into an hour, use np.arctan2(sin, cos) and scale the angle back by period / (2 * pi) [5]. I confirmed that this recovers hour 23 exactly.
How I Ran These
Every experiment uses the same generator: 6,000 hourly points drawn uniformly from 0 to 23, a target shape, and Gaussian noise at 30% of the target's standard deviation. I held out 25% for testing, fit Ridge regression, and used a fixed seed.
Because of that noise, the ceiling is about 0.92. A score of 0.916 is not a near-miss; it is the best any model can do on this data. That ceiling is the reason the first experiment ties.
Experiment 1: A Smooth Daily Cycle
I generated a smooth daily wave peaking at 14:00 and fit each encoding.
| Encoding | Columns | Test R² |
|---|---|---|
| Raw hour | 1 | 0.206 |
| One-hot | 24 | 0.916 |
| Sine and cosine | 2 | 0.916 |
| Three harmonics | 6 | 0.916 |
| Periodic splines | 8 | 0.916 |
Raw hours fail badly, as expected. But look at one-hot against sine and cosine: they score the same, and one uses 24 columns while the other uses 2. On smooth cycles, cyclical encoding gives you the same accuracy with a twelfth of the features.
Experiment 2: Where One-Hot Wins
Now the humbling part. Real patterns often have sharp peaks, like a morning rush and an evening rush. I generated a two-peak pattern, with peaks at 8 AM and 5 PM.
| Encoding | Columns | Test R² |
|---|---|---|
| Raw hour | 1 | 0.011 |
| One-hot | 24 | 0.914 |
| Sine and cosine, one harmonic | 2 | 0.129 |
| Three harmonics | 6 | 0.828 |
| Periodic splines | 8 | 0.804 |
A single sine and cosine pair can draw only one smooth wave per day. Two sharp peaks need more flexibility. One-hot handled it because it learns each hour separately, with no shape assumptions.
The fix keeps the idea but adds harmonics. Three harmonics recovered most of the accuracy with 6 columns. Each extra harmonic adds a faster wave, so you can approximate sharper shapes.
Experiment 3: Peaks That Straddle Midnight
I also tested a peak centred on midnight, where demand rises from 10 PM to 2 AM. This case shows the wrap-around problem most clearly.
| Encoding | Columns | Test R² |
|---|---|---|
| Raw hour | 1 | 0.038 |
| One-hot | 24 | 0.910 |
| Sine and cosine, one harmonic | 2 | 0.334 |
| Three harmonics | 6 | 0.782 |
| Periodic splines | 8 | 0.882 |
Periodic splines came within three points of one-hot using 8 columns instead of 24, and three harmonics were close behind. Splines deserve more attention than they get. In scikit-learn, SplineTransformer with extrapolation="periodic" and explicit knots over your cycle length builds them.
from sklearn.preprocessing import SplineTransformer
spl = SplineTransformer(
n_knots=9, degree=3, extrapolation='periodic',
knots=np.linspace(0, 24, 9).reshape(-1, 1),
)
X_hour = spl.fit_transform(df[['hour']].astype(float))
That configuration produces 8 basis columns, because periodic splines give you n_knots - 1 of them instead of n_knots + degree - 1 [2]. More knots buy sharper resolution, which is why this setup lagged on the twin peaks and nearly matched one-hot on the single midnight bump.
Experiment 4: Do Tree Models Need This?
No, mostly. I trained a histogram-based gradient boosting regressor on raw hours and on sine and cosine features. On all three patterns the scores matched to three decimals.
| Pattern | Raw hour | Sine and cosine |
|---|---|---|
| Smooth daily | 0.916 | 0.916 |
| Two peaks | 0.914 | 0.914 |
| Midnight peak | 0.910 | 0.910 |
Trees split the hour axis into ranges, so they can carve out "hours 22 to 23" and "hours 0 to 1" separately, then give both the same prediction. They do not need the circle. If you only use tree-based models, raw hours work fine, and cyclical encoding adds little. Linear models, neural networks and distance-based models benefit far more.
Experiment 5: Where Cyclical Encoding Really Pays Off
Small Datasets
One-hot encoding learns each category separately, so each hour needs its own data. I trained on different sample sizes and averaged the test R² over 30 random runs.
| Training rows | One-hot | Sine and cosine |
|---|---|---|
| 60 | 0.635 | 0.909 |
| 120 | 0.835 | 0.914 |
| 500 | 0.908 | 0.916 |
| 3,000 | 0.916 | 0.917 |
With plenty of data the two tie. With little data, sine and cosine pull ahead, because neighbouring hours share information through the smooth curve. At 60 training rows the gap was 0.27 R², which is the difference between a model you can ship and one you cannot.
High-Cardinality Cycles
Minute of day has 1,440 values. With 3,000 training rows, one-hot encoding created 1,136 columns and scored an R² of 0.602. Sine and cosine used 2 columns and scored 0.921. At that scale, one-hot becomes unwieldy and sparse, while the circle stays compact.
The same logic covers day of year, seconds within a minute, and wind direction in degrees.
Weekdays Work the Same Way
I tested a smooth weekly pattern. Raw weekday scored an R² of 0.000, while both one-hot and sine and cosine scored 0.922. For only seven categories the choice barely matters, and sine and cosine just saves you five columns.
When to Use Which
Here is my honest decision guide:
- Linear or neural models with smooth cycles: use sine and cosine.
- Sharp or irregular patterns: use one-hot, periodic splines, or sine and cosine with several harmonics.
- Tree-based models only: use raw numbers, and skip the extra work.
- Small datasets: prefer sine and cosine or splines, since they share strength between neighbours.
- Many categories, like minutes or days of year: use sine and cosine or splines.
- Interpretability matters: one-hot gives you one coefficient per hour, which people can read easily.
- Unsure: try one-hot, sine and cosine with 3 harmonics, and periodic splines, then compare with cross-validation.
Common Pitfalls
- Using the wrong period. Hours use 24, not 23. Months use 12 after you subtract 1, so January maps to 0. Day of year uses about 365.25.
- Letting a library guess the period. Feature-engine's
CyclicalFeaturestakes amax_valuesmapping and falls back to the maximum observed value, which is 23 for hours 0 to 23 [4]. Pass 24 explicitly. - Using only sine or only cosine. You then map two different times to the same value.
- Forgetting that one harmonic is a single smooth wave. Sharp patterns need more harmonics or another method.
- Encoding non-cyclical features. Age and distance do not wrap around, so leave them alone.
- Applying it to trees and expecting a boost. The gain is usually zero.
- Encoding timestamps inconsistently between training and production. Reuse the same function and the same period.
- Ignoring time zones. Convert to local time first if behaviour follows local hours.
Wrapping Up
Raw hours break the wrap-around, one-hot encoding ignores neighbours, and sine and cosine encoding fixes both with two columns. It matches one-hot on smooth cycles, wins on small data and fine-grained time, and loses on sharp patterns unless you add harmonics or switch to periodic splines. Tree models rarely care.
So take your time features, plot the target against the hour, and look at the shape. A smooth curve points to sine and cosine, and a spiky one points to harmonics, splines or one-hot. Then let cross-validation make the final call.
Frequently Asked Questions
Is sine and cosine encoding better than one-hot encoding for hours?
On smooth daily cycles they tie, and sine and cosine do it with 2 columns instead of 24. On sharp or irregular patterns one-hot wins unless you add harmonics or switch to periodic splines. On small datasets and high-cardinality cycles like minute of day, sine and cosine win clearly.
Why do I need both sine and cosine?
Sine alone maps two different times to the same value, because sin(x) equals sin(pi - x). For example, 3 AM and 9 AM both give 0.707. Cosine breaks the tie, so the pair identifies each point on the circle uniquely.
Do tree models need cyclical encoding?
Usually not. Trees split the hour axis into ranges and can isolate late-evening hours from early-morning hours without any wrap-around help. In my runs a histogram gradient boosting model scored identically on raw hours and on sine and cosine features.
What period should I use for hours, months and day of year?
Hours use 24, weekdays use 7, months use 12 after you subtract 1 so January maps to 0, and day of year uses about 365.25. Never use the maximum observed value as the period, because hours running 0 to 23 would give you 23 and put hour 23 back on top of hour 0.
How many harmonics should I use?
Start with one sine and cosine pair. If the target has sharp peaks, add harmonics and compare with cross-validation; three harmonics recovered most of the accuracy in my two-peak test. Each harmonic adds a faster wave and two more columns, so stop when the score flattens.
Sources
[1]scikit-learn, Time-related feature engineering — the official example that encodes periodic features withSplineTransformerandextrapolation="periodic", and that explains why a linear model needs engineered non-linear terms for a cycle: scikit-learn.org/stable/auto_examples/applications/plot_cyclical_feature_engineering.html[2]scikit-learn, SplineTransformer — the API reference forn_knots,degree, explicitknotsand theextrapolationoptions, including the note that the number of basis functions isn_knots + degree - 1, orn_knots - 1for periodic extrapolation: scikit-learn.org/stable/modules/generated/sklearn.preprocessing.SplineTransformer.html[3]scikit-learn, Preprocessing data — the user guide behind one-hot encoding,handle_unknown, sparse output and the infrequent-category options that exist for high-cardinality columns: scikit-learn.org/stable/modules/preprocessing.html[4]Feature-engine, CyclicalFeatures — the packaged transformer that returnsvar_sinandvar_cosper variable, computed assin(variable * (2 * pi / max_value)), withmax_valuesanddrop_originalparameters: feature-engine.trainindata.com/en/latest/api_doc/creation/CyclicalFeatures.html[5]NumPy, numpy.arctan2 — the two-argument arctangent used to recover the original angle, and therefore the original hour, from a sine and cosine pair: numpy.org/doc/stable/reference/generated/numpy.arctan2.html