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))
  1. import datetime

    1import datetime2date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']
  2. 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_strs
  3. years ← []

    2date_strs = ['2024-01-15', '2024-03-22', '2024-07-04', '2024-11-11']3years = []4months = []
    values this step[]years
  4. months ← []

    3years = []4months = []5for s in date_strs:
    values this step[]months
  5. s ← '2024-01-15'

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2024-01-15'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 ← [2024]

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

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

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2024-01-15' '2024-03-22'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 ← [2024, 2024]

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

    7    years.append(d.year)8    months.append(d.month)9print('RESULT:', (years, months))
    values this step[1] [1, 3]months
  13. s ← '2024-07-04'

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2024-03-22' '2024-07-04'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 ← [2024, 2024, 2024]

    6d = datetime.date.fromisoformat(s)7years.append(d.year)8months.append(d.month)
    values this step[2024, 2024] [2024, 2024, 2024]years
  16. months ← [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]months
  17. s ← '2024-11-11'

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
    values this step'2024-07-04' '2024-11-11'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 ← [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]years
  20. months ← [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]months
  21. for s in date_strs:

    4months = []5for s in date_strs:6    d = datetime.date.fromisoformat(s)
  22. 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 .dt accessor is available on any datetime64 Series. 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/.month which are also plain ints, so the RESULT strings compare equal without any conversion.
  • Cross-reference: parse-date-column (this chapter) to produce the datetime64 Series this lesson starts from.