Data Science · Level 3 Dates

pandas Dates

Convert order dates from text to real dates with pd.to_datetime(), then count the orders of each month with the .dt accessor.

  • pd.to_datetime() parses text
  • .dt.month and .dt.day_name()
  • Text dates sort wrongly

Dates read from a CSV file are text. Trap: as text, "10/02" sorts before "9/02". pd.to_datetime makes real dates, then .dt gives their parts, like .dt.month or .dt.day_name().

d = pd.to_datetime(pd.Series(["2024-03-15", "2024-11-02"]))
d.dt.month.tolist()       # [3, 11]
d.dt.day_name().tolist()  # ['Friday', 'Saturday']

Write orders_per_month(dates) that takes a Series of ISO date strings and returns a dict mapping each month number to its number of orders, for example ["2024-01-05", "2024-01-20", "2024-03-02"] gives {1: 2, 3: 1}.

solution.py
Test Suites Pending
Output