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
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.
naive.py
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)
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.