Dates and times in pandas

Beginner Data Science Praktikum
Created by Pavel · 03.04.2026 at 11:49 UTC · 1 completed

Dates arrive as strings, get stored as objects, and break when you try to subtract them. The entry point is always pd.to_datetime(), which parses strings like '2025-03-15' or '15/03/2025' into proper datetime64 values. Once converted, the .dt accessor opens up a clean API for extraction: df['date'].dt.year, .dt.month, .dt.dayofweek (0 = Monday, 6 = Sunday), .dt.quarter. Without to_datetime, these attributes do not exist and you are stuck splitting strings by hand.

For time series analysis, resample() is the groupby equivalent for time. After setting the date column as the index, df.resample('ME').sum() groups all rows within each calendar month and sums them. Common frequency strings: 'D' (daily), 'W' (weekly), 'ME' (month end), 'QE' (quarter end). If you need a regular date axis for testing, pd.date_range('2025-01-01', periods=12, freq='ME') generates twelve month-end timestamps.

Date arithmetic also becomes natural: (pd.Timestamp.now() - df['date']).dt.days gives you the age of each record in days. Subtracting two datetime columns produces a Timedelta Series — you can access .dt.total_seconds() or .dt.days without manual epoch math.

A common gotcha: if your CSV has dates like 01/02/2025, is that January 2nd or February 1st? Pass dayfirst=True to pd.to_datetime for European dd/mm/yyyy formats, or specify format='%d/%m/%Y' to be explicit.

Time series guide: [1], date_range docs: [2].


Sources

University approvals: 0
Tasks
Question 1

What does this code print?

import pandas as pd
s = pd.to_datetime(['2025-01-06', '2025-01-07'])
print(s.dayofweek.tolist())

(2025-01-06 is a Monday, 2025-01-07 is a Tuesday.)

Hint

pandas uses 0 = Monday, 6 = Sunday.

Question 2

After df = df.set_index('date'), what does df.resample('ME').sum() produce?

Hint

resample is a time-aware groupby; 'ME' means month-end.

Question 3

Using pandas, implement extract_months(csv_text: str) -> list that reads a CSV with a date column (strings in YYYY-MM-DD format), converts to datetime using pd.to_datetime, and returns the month numbers as a list of ints.

Example: date\n2025-01-15\n2025-03-01\n2025-12-25[1, 3, 12].

Submit the function; tests use expression mode.

Hint

pd.to_datetime() first, then .dt.month to extract the month number.

Starter code is prefilled; replace TODO blocks with your solution.
2 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Data Science Praktikum
  • Difficulty: Beginner
  • Completed: 1 users
Creator
Pavel
Pavel