Dates and Ordered Data
Parse Date Column
Split ISO date strings into (year, month, day) integer tuples by parsing
each string at the '-' delimiter. With pandas, pd.to_datetime converts a
string column to datetime64, enabling date arithmetic and .dt accessors.
By hand
Loop over date_strs, split each on '-', and convert the three parts to
ints. Building (year, month, day) tuples keeps the components together in
result and makes the parsed structure visible in each trace step.
naive.py
Replay: real traced execution (multi-file project)
date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']
result = []
for s in date_strs:
parts = s.split('-')
result.append((int(parts[0]), int(parts[1]), int(parts[2])))
print('RESULT:', result)
date_strs ← ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']
1date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']2result = []values this step['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']date_strsresult ← []
1date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']2result = []3for s in date_strs:values this step[]results ← '2024-01-15'
2result = []3for s in date_strs:4 parts = s.split('-')values this step'2024-01-15'sparts ← ['2024', '01', '15']
3for s in date_strs:4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))values this step['2024', '01', '15']partsresult ← [(2024, 1, 15)]
4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))6print('RESULT:', result)values this step[] → [(2024, 1, 15)]results ← '2024-03-22'
2result = []3for s in date_strs:4 parts = s.split('-')values this step'2024-01-15' → '2024-03-22'sparts ← ['2024', '03', '22']
3for s in date_strs:4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))values this step['2024', '01', '15'] → ['2024', '03', '22']partsresult ← [(2024, 1, 15), (2024, 3, 22)]
4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))6print('RESULT:', result)values this step[(2024, 1, 15)] → [(2024, 1, 15), (2024, 3, 22)]results ← '2024-07-04'
2result = []3for s in date_strs:4 parts = s.split('-')values this step'2024-03-22' → '2024-07-04'sparts ← ['2024', '07', '04']
3for s in date_strs:4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))values this step['2024', '03', '22'] → ['2024', '07', '04']partsresult ← [(2024, 1, 15), (2024, 3, 22), (2024, 7, 4)]
4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))6print('RESULT:', result)values this step[(2024, 1, 15), (2024, 3, 22)] → [(2024, 1, 15), (2024, 3, 22), (2024, 7, 4)]results ← '2024-11-11'
2result = []3for s in date_strs:4 parts = s.split('-')values this step'2024-07-04' → '2024-11-11'sparts ← ['2024', '11', '11']
3for s in date_strs:4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))values this step['2024', '07', '04'] → ['2024', '11', '11']partsresult ← [(2024, 1, 15), (2024, 3, 22), (2024, 7, 4), (2024, 11, 11)]
4 parts = s.split('-')5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))6print('RESULT:', result)values this step[(2024, 1, 15), (2024, 3, 22), (2024, 7, 4)] → [(2024, 1, 15), (2024, 3, 22), (2024, 7, 4), (2024, 11, 11)]resultfor s in date_strs:
2result = []3for s in date_strs:4 parts = s.split('-')stdout ← RESULT: [(2024, 1, 15), (2024, 3, 22), (2024, 7, 4), (2024, 11, 11)]
5 result.append((int(parts[0]), int(parts[1]), int(parts[2])))6print('RESULT:', result)values this stepRESULT: [(2024, 1, 15), (2024, 3, 22), (2024, 7, 4), (2024, 11, 11)]stdout
With pandas
pd.to_datetime(df['date']) parses the string column into a datetime64[ns]
Series in one call. The snapshot shows the ISO-formatted values via
.dt.strftime and the dtype; result extracts integer components for
deterministic comparison with the naive half.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']
df = pd.DataFrame({'date': date_strs})
s = pd.to_datetime(df['date'])
result = list(zip(s.dt.year.tolist(), s.dt.month.tolist(), s.dt.day.tolist()))
print('index:', s.index.tolist())
print('values:', s.dt.strftime('%Y-%m-%d').tolist())
print('dtype:', s.dtype)
print('RESULT:', result)
index: [0, 1, 2, 3]
values: ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']
dtype: datetime64[ns]
RESULT: [(2024, 1, 15), (2024, 3, 22), (2024, 7, 4), (2024, 11, 11)]
Implementation notes
pd.to_datetimeinfers common date formats automatically. For ambiguous or non-standard strings, passformat='%Y-%m-%d'to be explicit and faster.- Once parsed to
datetime64, the column gains.dtaccessors for components and arithmetic — seeextract-date-parts. - Date equality: both halves extract plain Python
inttuples — avoiding any mismatch between stdlibdatetime.dateobjects and pandasTimestamp. - Cross-reference:
extract-date-parts(this chapter) for.dtaccessor usage after parsing.