Feature Engineering for Dates & Text
Part of the Machine Learning Roadmap
In short
A timestamp like 2026-10-24 21:40 means nothing to a model. 'Saturday night, about two weeks before a big festival' means a lot. Turning raw dates and text into meaningful numbers is often worth more than switching algorithms.
Raw dates and text rarely help a model directly. Dates become features like day of week, month, holiday flags and days since an event, and text becomes length, keyword flags or TF-IDF vectors.

Think of it like this
The sweet shop owner's calendar
A halwai doesn't plan stock by the raw date. He thinks: is it a weekend, is a festival coming, is it wedding season, is it evening rush? From customer notes he picks up keywords like 'too sweet' or 'fresh'. Feature engineering is teaching the model to read the calendar and the comments the way he does.
Watch it happen
One raw row becomes many features
1/5Order placed at 2026-10-24 21:40 with the review 'Late delivery and cold food'.
The big ideas
What you need to know
Break dates into parts
Hour, day of week, month, weekend flag and quarter capture rhythms that a raw timestamp hides.
Cyclic features
Hour 23 and hour 0 are neighbours. Encoding hour as sine and cosine keeps that closeness.
Time since and until
Days since signup, days since last order, days until a festival. Gaps often predict better than dates.
Text into numbers
Start with length and keyword flags, then TF-IDF to weight words that are frequent in one text but rare overall.
See it in code
Walk through the code
1import numpy as np2import pandas as pd3from sklearn.feature_extraction.text import TfidfVectorizer4 5df = pd.DataFrame({6 "order_time": pd.to_datetime(["2026-10-20 13:05", "2026-10-24 21:40", "2026-11-01 09:15"]),7 "review": ["Fast delivery, food was hot", "Late delivery and cold food", "Great taste, will order again"],8})9festival = pd.Timestamp("2026-11-08")10 11df["hour"] = df["order_time"].dt.hour12df["day_of_week"] = df["order_time"].dt.dayofweek # Monday = 013df["is_weekend"] = (df["day_of_week"] >= 5).astype(int)14df["hour_sin"] = np.sin(2 * np.pi * df["hour"] / 24)15df["hour_cos"] = np.cos(2 * np.pi * df["hour"] / 24)16df["days_to_festival"] = (festival - df["order_time"]).dt.days17 18df["review_words"] = df["review"].str.split().str.len()19df["mentions_late"] = df["review"].str.contains("late", case=False).astype(int)20 21tfidf = TfidfVectorizer(stop_words="english")22text_matrix = tfidf.fit_transform(df["review"])23print(df.drop(columns=["order_time", "review"]))24print(tfidf.get_feature_names_out())1/5The .dt accessor pulls parts out of a datetime column. dayofweek 5 and 6 are the weekend.
Date parts, a cyclic hour, a days-until-festival gap and three kinds of text features.
Save yourself the pain
Common mistakes
Oops
Feeding the raw timestamp as one big number.
Do this instead
Extract the parts and gaps that relate to the target. The model can't guess that Saturdays matter from a Unix timestamp.
Oops
Fitting TfidfVectorizer on train and test together.
Do this instead
Fit it on training text only, then transform test text, ideally inside a Pipeline.
Your mission
Engineer ten features
Quick check
Why encode 'hour of day' with sine and cosine?
Go deeper
- 1Extract day of week, month and is_weekend from an order date column.
- 2Add a 'days since last purchase' feature for a customer table.
- 3Turn a short text column into TF-IDF features with TfidfVectorizer.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.