Time and categorical data
Quick answer
Time features need a known timestamp format, timezone and prediction cutoff. Categorical features represent named groups with a defined set of allowed values, not necessarily numeric order. Parse time explicitly and preserve unknown categories as a quality signal rather than assigning them a misleading numeric meaning. Examples run locally on Python 3.13.5 and pandas 2.2.3; ByteMind package support is not verified. All data are synthetic.
Parse UTC timestamps and inspect elapsed time
Use format for known input and utc=True for the agreed UTC convention. errors="coerce" turns invalid dates into NaT; audit those failures.
import pandas as pdraw = pd.Series(['2025-01-01 10:00', '2025-01-01 12:30', 'bad'])t = pd.to_datetime(raw, format='%Y-%m-%d %H:%M', utc=True, errors='coerce')print(t.isna().tolist())print((t.iloc[1] - t.iloc[0]).total_seconds() / 60)print(t.dt.hour.iloc[:2].tolist())Expected output (pandas 2.2.3):
[False, False, True]150.0[10.0, 12.0]The first two readings are 150 minutes apart. The third is not a valid timestamp. Naive input is interpreted as UTC here because that is the fixture contract; real local time needs its known timezone and daylight-saving policy.
Apply a fixed category vocabulary
The allowed categories are defined in advance, or learned from training data only. Casting unknown values to this dtype marks them missing, so audit before conversion.
import pandas as pdraw = pd.Series(['low', 'high', 'unknown', None])levels = pd.CategoricalDtype(['low', 'medium', 'high'], ordered=True)s = raw.astype(levels)print(s.cat.categories.tolist())print(s.cat.codes.tolist())print((raw.notna() & s.isna()).tolist())Expected output (pandas 2.2.3):
['low', 'medium', 'high'][0, 2, -1, -1][False, False, True, False]The codes are positions in the specified category list, with -1 for missing. Unknown and original missing both become missing after casting, but the audit separates them. Even ordered codes do not establish equal spacing between levels.
Common mistakes
Do not parse ambiguous dates without a format. Sort by time before sequential calculations and define ties. Do not derive features from events after the prediction cutoff. Nominal categories such as site IDs should not be treated as ordinal measurements; choose encoding later with training-only vocabulary and a defined unknown policy.
Practice and next steps
Audit invalid timestamps and unseen device types in a new fixture; decide what to do with each rather than silently deleting rows. Try the pure-Python reasoning challenge and knowledge check separately; these do not assess pandas API execution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Time and categorical data — knowledge check
Apply tabular data reasoning to new synthetic fixtures.
Which value represents an invalid datetime after coercion?
What does utc=True do to naive timestamps in this example?
What code does pandas use for a missing categorical value?
Why not treat nominal site codes as measurements?
When may an event be used to form a prediction feature?