Sam Austinon October 31, 202611 min read

Day of Week in Decision Trees: Do You Need to Encode It?

Day of Week in Decision Trees: Do You Need to Encode It?
Contents

Figure 1: Day of Week in Decision Trees: Do You Need to Encode It? — a tree can survive a lazy weekday encoding, but surviving and doing well are different things

Short answer: sometimes. Longer answer: a tree can survive a lazy encoding, but "survive" and "do well" are not the same thing. Here is where the shortcut works and where it bites.

The Lazy Option

You have a day_of_week column, and pandas hands you integers from 0 (Monday) to 6 (Sunday). Trees only ask yes/no questions like "is the value ≤ 3.5?", so the integers feel fine [4]. Why bother with anything fancier?

The catch: that column quietly tells the tree that Sunday (6) is far from Monday (0) and that the days sit in one fixed order. Real weeks wrap around, and real weekday effects rarely behave like a number line.

I tested this on synthetic data, a made-up level per weekday plus noise, so treat the numbers below as a demonstration rather than a benchmark.

What I Tested

The setup: each weekday has its own average level, plus noise. The levels run Monday to Sunday as 10, 2, 3, 2, 10, 11, 10. So Friday through Monday is high, and Tuesday through Thursday is low. That high block wraps around the end of the week, which is the kind of shape a number line handles badly.

I fit decision trees at different depths with three encodings:

  • Ordinal: the raw 0–6 integers.
  • One-hot: seven yes/no columns [5].
  • Sin/cos: two columns that place the days on a circle.

I used 3,000 training rows and 3,000 test rows. The noise variance is 2.25, so a model that knows the true levels still scores about 2.2 on a finite test set. Treat 2.2 as the floor.

What Happened

Max depth Ordinal One-hot Sin/cos
1 8.52 13.58 2.42
2 2.37 8.11 2.22
3 2.30 2.32 2.22
4 2.22 2.22 2.22
no limit 2.22 2.22 2.22

Test MSE, lower is better. Three things stand out.

  1. Depth fixes almost everything. By depth 4, all three encodings hit the noise floor. A deep tree can cut any encoding into the right pieces.
  2. Shallow trees care a lot. At depth 1, the ordinal tree scored 8.52 and the one-hot tree scored 13.58, against a floor near 2.2.
  3. One-hot looked worst when shallow. That surprised me. One-hot columns let each cut isolate only one day, so a single split cannot group four days together.

So do you need to encode it? If your trees are deep, the encoding barely matters on a pattern like this. If you use shallow trees, it matters a great deal.

Why Sin/Cos Won at Depth 1

Sin/cos scored 2.42 at depth 1, nearly the noise floor, with one cut. Why?

On the circle, Friday to Monday forms one contiguous arc. One cut across a coordinate axis separates that arc from the rest. The ordinal line cannot do that, because the block is broken in two at the ends of the line.

Before you crown sin/cos, look at the second pattern I tested. Only Tuesday and Saturday were high, and they are not neighbours on the circle:

Max depth Ordinal One-hot Sin/cos
1 14.38 9.97 9.97
2 6.75 2.22 6.75
3 2.22 2.22 2.22

Now sin/cos lost its edge at depth 1, and one-hot won at depth 2. The lesson is that no encoding wins everywhere. Which one wins depends on the shape of your weekday effect.

Native Categorical Support: The Shortcut That Works

There is a fourth option that skips the encoding debate. Scikit-learn's HistGradientBoostingRegressor can treat a column as categorical [2]. The docs say categorical_features="from_dtype" is the default, since version 1.6, and pandas columns with a Categorical dtype then count as categorical.

I compared one boosted tree, a single iteration with learning rate 1.0, so it behaves like one tree, on ordinal integers versus a categorical column:

Max depth Ordinal Native categorical
1 8.52 2.42
2 2.37 2.22

At depth 1, the categorical version matched the best encoding in my table above. A single categorical split can group several days at once, so it can isolate the whole high block no matter how the days are ordered [3].

Honest caveats from the docs [2]:

  • Each categorical feature must have at most max_bins unique categories. Seven weekdays fit easily.
  • Categorical values convert to floats, so 1.0 and 1 count as the same category.
  • Negative values in numeric categorical columns count as missing.

I did not find a categorical option in the DecisionTreeRegressor docstring I read, so for plain scikit-learn trees you still choose an encoding yourself [1].

The Linear Contrast

If you use a linear model, the answer flips. I ran Ridge on the first pattern:

Encoding Test MSE
Ordinal 14.30
Sin/cos 4.83
One-hot 2.22

A linear model draws one straight line through 0–6, so it cannot represent a weekday that is high, low, then high again. One-hot lets it assign each day its own level. That is why advice like "just use integers" circulates: it is often true for trees and often wrong for everything else.

So What Should You Do?

Here is the decision list I would use:

  1. Deep trees or forests with plenty of data: raw integers are probably fine. Check by comparing against one-hot on your own validation set.
  2. Shallow trees or boosting with small depth: try sin/cos if your pattern looks smooth around the week, and one-hot if specific days behave differently.
  3. Gradient boosting in scikit-learn: use native categorical support. It removes the encoding question.
  4. Linear models or neural nets: one-hot, or splines if you want something smoother.
  5. Anything involving the weekend: consider an is_weekend flag. It is cheap, readable, and gives even a depth-1 tree something useful.

The best habit is also the least glamorous: try two encodings and let validation score decide.

Common Pitfalls

  1. Assuming integers are always wrong. Deep trees handle them fine on easy patterns.
  2. Assuming integers are always fine. Shallow trees and linear models do not.
  3. Testing encodings on a shuffled split when your data is temporal. This test is not temporal, but your production problem probably is.
  4. Comparing encodings without fixing the tree depth. Depth changes the answer more than encoding does.
  5. Reading a 2.2 as an improvement over a 2.25 floor. That difference is sampling noise.
  6. Forgetting that sin/cos assumes a circular shape. If your high days are not contiguous, it will not help.

Wrapping Up

Do decision trees need an encoded weekday? A deep tree on an easy pattern shrugs it off. A shallow tree does not, and a linear model definitely does not. The raw 0–6 integers pretend the week is a line, so ask whether your data agrees.

Next time you add a weekday column, run the 10-minute test: fit your model with integers, one-hot and sin/cos at your real tree depth. Keep whichever wins on a proper validation split.

Frequently Asked Questions

Do decision trees need one-hot encoding for day of week?

Usually not, if the tree is deep. A deep tree can split the raw 0 to 6 integers into as many pieces as it needs, and by depth 4 every encoding I tested hit the noise floor. Shallow trees are a different story: at depth 1 the raw integers scored 8.52 against 2.42 for sin/cos.

Why did one-hot encoding perform worst at depth 1?

A one-hot column is a yes/no flag for a single day, so one split can isolate only that day. The weekday pattern I tested had a block of four high days, which needs four splits to separate in one-hot form. Ordinal integers can at least cut the line in one place, and sin/cos can cut an arc.

Is sin/cos encoding always better for cyclical features?

No. It won on a pattern where the high days formed a contiguous arc around the week, and lost its edge when the high days were Tuesday and Saturday, which are not neighbours on the circle. The encoding has to match the shape of the effect.

Does scikit-learn support categorical features in decision trees?

Not in DecisionTreeRegressor, which has no categorical_features argument. HistGradientBoostingRegressor does: categorical_features defaults to from_dtype since version 1.6, so pandas columns with a Categorical dtype are treated as categories and can be grouped in a single split.

What about linear models and weekdays?

A linear model draws one straight line across 0 to 6, so it cannot represent high, low, high. Ridge scored 14.30 on raw integers, 4.83 on sin/cos and 2.22 on one-hot. The advice to just use integers is often right for trees and usually wrong for everything else.

Sources

What are You Looking For?

esc