Skip to content

Date and text features

Turn dates into parts and durations, keep cycles circular, and turn text into numbers a model can use.

Applied ~10 min9 steps

You will be able to

  • Extract date parts and durations
  • Encode a cycle so its ends meet
  • Choose between simple text features and term columns

Read first

01

A date holds many features

Read

A date is only useful once you ask something of it. In Prepare (⌘3):

  • Date parts gives year, quarter, month, day, ISO weekday and week, hour, day of year and a weekend flag.
  • Time between dates gives days, hours or weeks from a reference to a date.

"Days since signup" often predicts more than "signup date", because it's what the date means for that customer today.

02

Is 11 pm far from midnight?

Predict

Predict first

You store hour of day as a number from 0 to 23. In reality, how far apart are 11 pm (hour 23) and midnight (hour 0)?

Close, one hour apartFar, 23 apartIt depends on the model
Reveal

The answer: Close, one hour apart

They're an hour apart, but the numbers say 23, and a linear model believes the numbers. Cyclical encoding replaces the hour with a sine and a cosine, so the ends of the cycle meet.

xsin⁡=sin⁡2πh24,xcos⁡=cos⁡2πh24x_{\sin} = \sin\frac{2\pi h}{24}, \quad x_{\cos} = \cos\frac{2\pi h}{24}
03

Try a date feature

In the app

Add the customer list, then start a recipe with these steps:

  1. Parse dates on signup_date
  2. Time between dates from signup_date to a fixed date
  3. Date parts for the weekday
Messy customer list

Look at the output grid. Then ask which of the new columns has a reason to matter for your question.

04

Simple text features

Read

Long before a language model, text gives you easy numbers. Text features adds length, word count and counts of digits, capitals and punctuation. Flag text matching a pattern adds a true/false column, such as "mentions a refund".

These are cheap, explainable and often surprisingly useful.

05

Term columns

Explore

Text to term columns makes one column per frequent term: counts, presence or TF-IDF, with a vocabulary learned from the data.

Try it on the notes column, and look at which terms become columns. Most rows there are empty, which is itself a finding, so consider a Flag missing values step too.

The vocabulary is a learned value. For a model, learn it on training rows only.

06

Look ahead

Read
07

In code

Read
Python
import numpy as np
import pandas as pd

df = pd.DataFrame({"when": pd.to_datetime(["2024-01-05 23:00", "2024-01-06 00:00"])})
df["weekday"] = df["when"].dt.dayofweek
hour = df["when"].dt.hour
df["hour_sin"] = np.sin(2 * np.pi * hour / 24)
df["hour_cos"] = np.cos(2 * np.pi * hour / 24)
R
library(dplyr)
library(lubridate)

df <- tibble(when = ymd_hm(c("2024-01-05 23:00", "2024-01-06 00:00")))
df <- df |>
  mutate(
    weekday = wday(when, week_start = 1),
    hour_sin = sin(2 * pi * hour(when) / 24),
    hour_cos = cos(2 * pi * hour(when) / 24)
  )
08

Check your understanding

Checkpoint

Check yourself

With a 24-hour cycle, what is sin⁡(2π⋅6/24)\sin(2\pi \cdot 6 / 24)? That's the sine feature at 6 am.

Show the answer

Answer: 1

2π⋅6/24=π/22\pi \cdot 6 / 24 = \pi/2, and sin⁡(π/2)=1\sin(\pi/2) = 1.

Check yourself

Which feature is safe for predicting next month's churn?

Show the answer

Answer: a

The other two are computed from the outcome or from the future. They won't exist when you need the prediction.

09

Recap

Recap
  • Dates become useful as parts and durations.
  • Encode cycles with a sine and a cosine.
  • Start with simple text features, and learn any vocabulary on training rows.