Dates and Ordered Data
Extract Date Parts
Pull year and month from a sequence of date strings into separate lists. By
hand, parse each string with datetime.date.fromisoformat and read .year
and .month. With pandas, .dt.year and .dt.month extract all values
from a datetime64 Series without looping.
By hand
Parse each ISO string to a datetime.date via fromisoformat, then append
.year and .month to separate accumulator lists. The date object is a
stdlib type (not in the traced types), so the trace shows the string s,
and the growing years and months lists.
naive.py
Replay: real traced execution (multi-file project)
import datetime
date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']
years = []
months = []
for s in date_strs:
d = datetime.date.fromisoformat(s)
years.append(d.year)
months.append(d.month)
print('RESULT:', (years, months))
import datetime
1import datetime2date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']date_strs ← ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']
1import datetime2date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']3years = []values this step['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']date_strsyears ← []
2date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']3years = []4months = []values this step[]yearsmonths ← []
3years = []4months = []5for s in date_strs:values this step[]monthss ← '2024-01-15'
4months = []5for s in date_strs:6 d = datetime.date.fromisoformat(s)values this step'2024-01-15'sd = datetime.date.fromisoformat(s)
5for s in date_strs:6 d = datetime.date.fromisoformat(s)7 years.append(d.year)years ← [2024]
6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)values this step[] → [2024]yearsmonths ← [1]
7 years.append(d.year)8 months.append(d.month)9print('RESULT:', (years, months))values this step[] → [1]monthss ← '2024-03-22'
4months = []5for s in date_strs:6 d = datetime.date.fromisoformat(s)values this step'2024-01-15' → '2024-03-22'sd = datetime.date.fromisoformat(s)
5for s in date_strs:6 d = datetime.date.fromisoformat(s)7 years.append(d.year)years ← [2024, 2024]
6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)values this step[2024] → [2024, 2024]yearsmonths ← [1, 3]
7 years.append(d.year)8 months.append(d.month)9print('RESULT:', (years, months))values this step[1] → [1, 3]monthss ← '2024-07-04'
4months = []5for s in date_strs:6 d = datetime.date.fromisoformat(s)values this step'2024-03-22' → '2024-07-04'sd = datetime.date.fromisoformat(s)
5for s in date_strs:6 d = datetime.date.fromisoformat(s)7 years.append(d.year)years ← [2024, 2024, 2024]
6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)values this step[2024, 2024] → [2024, 2024, 2024]yearsmonths ← [1, 3, 7]
7 years.append(d.year)8 months.append(d.month)9print('RESULT:', (years, months))values this step[1, 3] → [1, 3, 7]monthss ← '2024-11-11'
4months = []5for s in date_strs:6 d = datetime.date.fromisoformat(s)values this step'2024-07-04' → '2024-11-11'sd = datetime.date.fromisoformat(s)
5for s in date_strs:6 d = datetime.date.fromisoformat(s)7 years.append(d.year)years ← [2024, 2024, 2024, 2024]
6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)values this step[2024, 2024, 2024] → [2024, 2024, 2024, 2024]yearsmonths ← [1, 3, 7, 11]
7 years.append(d.year)8 months.append(d.month)9print('RESULT:', (years, months))values this step[1, 3, 7] → [1, 3, 7, 11]monthsfor s in date_strs:
4months = []5for s in date_strs:6 d = datetime.date.fromisoformat(s)stdout ← RESULT: ([2024, 2024, 2024, 2024], [1, 3, 7, 11])
8 months.append(d.month)9print('RESULT:', (years, months))values this stepRESULT: ([2024, 2024, 2024, 2024], [1, 3, 7, 11])stdout
With pandas
After pd.to_datetime, use s.dt.year.tolist() and s.dt.month.tolist() to
extract all year and month values in a single vectorized step per component.
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'])
years = s.dt.year.tolist()
months = s.dt.month.tolist()
print('index:', s.index.tolist())
print('years:', years)
print('months:', months)
print('RESULT:', (years, months))
index: [0, 1, 2, 3]
years: [2024, 2024, 2024, 2024]
months: [1, 3, 7, 11]
RESULT: ([2024, 2024, 2024, 2024], [1, 3, 7, 11])
Implementation notes
- The
.dtaccessor is available on anydatetime64Series. Common components:.dt.year,.dt.month,.dt.day,.dt.hour,.dt.dayofweek(Monday=0),.dt.day_name(),.dt.quarter. tolist()converts int64 component Series to Python ints — matching stdlib.year/.monthwhich are also plain ints, so the RESULT strings compare equal without any conversion.- Cross-reference:
parse-date-column(this chapter) to produce thedatetime64Series this lesson starts from.