Data types: numbers, categories, text and dates

Each column type needs different handling; spotting the right type is the first modeling decision.

Lesson 6 of 6610 minBeginner

You will learn to

  • Tell numeric, categorical, ordinal, text and date columns apart
  • Spot numbers that are really categories
  • Choose a first preprocessing step per type

Intuition

A spreadsheet cell holds a value, but the same digits can mean very different things. The number 28001 might be a price, a number of visitors or a zip code. In the first two cases it is a quantity and averaging makes sense. As a zip code it is just a name for a place. A model that treats the zip code as a quantity will look for a pattern in "bigger zip codes", which is nonsense.

That is why the first modeling decision is not the algorithm. It is deciding what kind of thing each column is, because the type decides which operations make sense and how the column must be prepared.

Five types cover most columns:

  • Numeric. A quantity where distances and averages mean something: age, price, temperature, number of visits.
  • Categorical (nominal). A label from a set with no order: country, colour, product type.
  • Ordinal. A label with a natural order but without equal steps: small, medium, large; or a satisfaction score from "poor" to "excellent".
  • Text. Free-form language: reviews, comments, descriptions.
  • Date and time. A point in time, which carries structure inside it: year, month, weekday, hour.

Two further kinds of column deserve a mention. Booleans (yes or no) behave like a category with two values. Identifiers (customer number, order id) are unique labels that name a row but say nothing about it.

Picture it

Numeric34.5, 120, 0.02First stepCheck range;scale if neededCategoricalred, blue, greenFirst stepOne-hot encodeOrdinallow < mid < highFirst stepMap to ranks 1,2, 3Textfree-form commentsFirst stepTurn into countsor dropDate2026-03-14First stepExtract parts:month, weekday
The five column types, an example of each, and the usual first preparation step. These are starting points, not fixed rules.

The traps come from columns that look like one type and are another:

Looks likeReally isWhy it matters
Zip code, phone area codeCategoryArithmetic on labels is meaningless
Customer id, order numberIdentifierUnique per row; a model can only memorise it
A rating of 1 to 5Ordinal (often treated as numeric)The step from 1 to 2 may not equal the step from 4 to 5
A year such as 2026Numeric or date, depending on useCheck whether trend over time is the point
0 and 1 flagsBoolean (a category)The mean is a share, not a typical value

The math

Preparation turns each type into numbers a model can use. Three small formulas cover the common cases.

One-hot encoding of a category with kk distinct values creates kk columns of 0 and 1. A row with value jj gets a 1 in column jj and 0 elsewhere. There is no order between the columns, which is exactly right for nominal data.

Ordinal encoding maps ordered values to ranks, for example small, medium, large to 1, 2, 3. It assumes that the steps are about equal, so check that this is reasonable.

Cyclic encoding is for values that wrap around, such as the month mm in a year of 12 months. Replace mm with two columns:

sin⁡ ⁣(2πm12),cos⁡ ⁣(2πm12)\sin\!\left(\frac{2\pi m}{12}\right), \qquad \cos\!\left(\frac{2\pi m}{12}\right)

Each month becomes a point on a circle, and neighbouring months are neighbouring points, including December and January. The same trick works for weekday (period 7) and hour (period 24).

Worked example

Take an illustrative customer table with eight columns and decide the type and the first step for each.

ColumnExample valuesTypeFirst step
customer_idC-10492IdentifierExclude from the model
age34, 61NumericCheck the range; scale if the algorithm needs it
zip_code28001, 08002CategoryOne-hot encode, or group into regions
planbasic, standard, premiumOrdinalMap to 1, 2, 3
countryES, FR, PT, ITCategoryOne-hot encode (4 columns)
signup_date2026-03-14DateExtract month and weekday; compute days since signup
comment"slow support"TextDrop at first, or count words
churnedyes, noBooleanThis is the target to predict

Check the cyclic encoding. For month 12 (December) the pair is (sin⁡2π,cos⁡2π)=(0,1)(\sin 2\pi, \cos 2\pi) = (0, 1). For month 1 (January) it is (sin⁡30∘,cos⁡30∘)=(0.5,0.866)(\sin 30^\circ, \cos 30^\circ) = (0.5, 0.866). The straight-line distance between these two points is 0.52+(1−0.866)2=0.518\sqrt{0.5^2 + (1 - 0.866)^2} = 0.518, which is the same as between any two neighbouring months. December and June, on opposite sides of the circle, are at distance 2. As plain numbers, December and January would differ by 11 and be treated as far apart.

Count the columns. After these steps the model sees: age (1), plan (1), zip_code (one column per distinct code), country (4), month as sine and cosine (2), weekday (as needed), days since signup (1). A high-cardinality column like zip_code is the one to watch, because it can add thousands of columns. Grouping rare values or using a coarser region is often better.

How mlflow does it

The data profile reads every column and assigns one of five Type chips: numeric, categorical, datetime, boolean or text. To see it, open Data, choose a dataset and select Explore, then open the Columns tab. You can search columns, filter by type and expand a column to see statistics, top values or sample values.

Things to know:

  • The profile guesses the type from the values. It does not know that a column of digits is a zip code, so check the chips yourself. There is no separate ordinal type; an ordered category appears as categorical, and you decide how to encode it.
  • Columns that are unique on every row are flagged ID-like, and columns with one value for all rows are flagged Constant. Neither helps a model.
  • In Pipelines, the Choose... list for a column groups transformations as Fill missing values, Scale numbers and Turn categories into numbers, which line up with the first steps in the table above.

Common mistakes

  • Treating codes as numbers. Zip codes, product codes and similar labels look numeric. A model will look for order that is not there. Convert them to categories.
  • Leaving an identifier in. A unique id lets a flexible model memorise rows. It looks excellent in training and fails on new customers. Exclude identifiers.
  • One-hot encoding a column with thousands of values. It creates a huge, sparse table and slows everything. Group rare values or use a higher-level label.
  • Encoding ordered values as unordered. One-hot encoding "small, medium, large" throws away the order. Use ranks when the order carries information.
  • Using a raw date. A timestamp as one big number is almost useless. Extract the parts that matter (month, weekday, days since an event).
  • Trusting the detected type blindly. Automatic detection is a first guess. A quick look at a few values per column catches the wrong ones.

Try it in mlflow

Open a dataset profile

  1. Open Data, find a dataset and choose Explore.
  2. Open the Columns tab and read the Type chip of every column.
  3. Find one column whose detected type is not what you would expect, such as a zip code stored as a number, and decide how it should be treated.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. A column called zip_code holds values such as 28001 and 08002. How should a model treat it?
  2. 2. A column 'size' has the values small, medium and large. What kind of column is it, and what is a sensible encoding?
  3. 3. Why is the month often encoded as a pair of sine and cosine values instead of as the numbers 1 to 12?