Dates
Extract Year and Month
Pull the year and month out of a column of ISO date strings into separate
lists. By hand, parse each string with datetime.date.fromisoformat and
read .year and .month. With pandas, pd.to_datetime followed by
.dt.year and .dt.month extracts the components without looping.
By hand
With pandas
pd.to_datetime(df['d']) parses the ISO strings to datetime64[ns].
.dt.year.tolist() and .dt.month.tolist() extract both components in
single vectorized steps. The extracted lists are plain Python ints, so
RESULT comparison with the naive half is exact without conversion.
naive.py
import datetime
date_strs = ['2023-05-10', '2023-08-25', '2024-01-03', '2024-06-18']
years = []
months = []
for s in date_strs:
d = datetime.date.fromisoformat(s)
years.append(d.year)
months.append(d.month)
print('RESULT:', (years, months))
library.py
import pandas as pd
from dalib.display import set_display
set_display()
date_strs = ['2023-05-10', '2023-08-25', '2024-01-03', '2024-06-18']
df = pd.DataFrame({'d': date_strs})
s = pd.to_datetime(df['d'])
years = s.dt.year.tolist()
months = s.dt.month.tolist()
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('years:', years)
print('months:', months)
print('RESULT:', (years, months))
index: [0, 1, 2, 3]
dtype: datetime64[ns]
years: [2023, 2023, 2024, 2024]
months: [5, 8, 1, 6]
RESULT: ([2023, 2023, 2024, 2024], [5, 8, 1, 6])
Implementation notes
.dt.yearand.dt.monthreturnint64Series;.tolist()converts each to plain Pythonint, matchingdatetime.date.year/.monthwhich are also plain ints — the RESULT tuples compare equal without any casting.- Other useful
.dtcomponents:.dt.day,.dt.dayofweek(Monday=0),.dt.quarter,.dt.day_name(). - Cross-reference:
extract-date-parts(python-pandas ch09) for the same.dtextraction pattern in the context of the pandas book's date workflow.