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

Parse each ISO string to a datetime.date with fromisoformat, then append .year and .month to their respective accumulators. The datetime.date object is not a traced type — the trace shows years and months growing one entry at a time.

naive.py
Replay: real traced execution (multi-file project)
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))
  1. import datetime

    1import datetime2date_strs = ['2023-05-10', '2023-08-25', '2024-01-03', '2024-06-18']
  2. date_strs ← ['2023-05-10', '2023-08-25', '2024-01-03', '2024-06-18']

    1import datetime2date_strs = ['2023-05-10', '2023-08-25', '2024-01-03', '2024-06-18']3years = []
    values this step['2023-05-10', '2023-08-25', '2024-01-03', '2024-06-18']date_strs
  3. years ← []

    2date_strs = ['2023-05-10', '2023-08-25', '2024-01-03', '2024-06-18']3years = []4months = []
    values this step[]years
  4. months ← []

    3years = []4months = []5for s in date_strs:
    values this step[]months
  5. s ← '2023-05-10'

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2023-05-10's
  6. d = datetime.date.fromisoformat(s)

    5for s in date_strs:6    d = datetime.date.fromisoformat(s)7    years.append(d.year)
  7. years ← [2023]

    6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)
    values this step[] [2023]years
  8. months ← [5]

    7    years.append(d.year)8    months.append(d.month)9print('RESULT:', (years, months))
    values this step[] [5]months
  9. s ← '2023-08-25'

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2023-05-10' '2023-08-25's
  10. d = datetime.date.fromisoformat(s)

    5for s in date_strs:6    d = datetime.date.fromisoformat(s)7    years.append(d.year)
  11. years ← [2023, 2023]

    6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)
    values this step[2023] [2023, 2023]years
  12. months ← [5, 8]

    7    years.append(d.year)8    months.append(d.month)9print('RESULT:', (years, months))
    values this step[5] [5, 8]months
  13. s ← '2024-01-03'

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2023-08-25' '2024-01-03's
  14. d = datetime.date.fromisoformat(s)

    5for s in date_strs:6    d = datetime.date.fromisoformat(s)7    years.append(d.year)
  15. years ← [2023, 2023, 2024]

    6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)
    values this step[2023, 2023] [2023, 2023, 2024]years
  16. months ← [5, 8, 1]

    7    years.append(d.year)8    months.append(d.month)9print('RESULT:', (years, months))
    values this step[5, 8] [5, 8, 1]months
  17. s ← '2024-06-18'

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2024-01-03' '2024-06-18's
  18. d = datetime.date.fromisoformat(s)

    5for s in date_strs:6    d = datetime.date.fromisoformat(s)7    years.append(d.year)
  19. years ← [2023, 2023, 2024, 2024]

    6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)
    values this step[2023, 2023, 2024] [2023, 2023, 2024, 2024]years
  20. months ← [5, 8, 1, 6]

    7    years.append(d.year)8    months.append(d.month)9print('RESULT:', (years, months))
    values this step[5, 8, 1] [5, 8, 1, 6]months
  21. for s in date_strs:

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
  22. stdout ← RESULT: ([2023, 2023, 2024, 2024], [5, 8, 1, 6])

    8    months.append(d.month)9print('RESULT:', (years, months))
    values this stepRESULT: ([2023, 2023, 2024, 2024], [5, 8, 1, 6])stdout

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.

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.year and .dt.month return int64 Series; .tolist() converts each to plain Python int, matching datetime.date.year / .month which are also plain ints — the RESULT tuples compare equal without any casting.
  • Other useful .dt components: .dt.day, .dt.dayofweek (Monday=0), .dt.quarter, .dt.day_name().
  • Cross-reference: extract-date-parts (python-pandas ch09) for the same .dt extraction pattern in the context of the pandas book's date workflow.