We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Working With Date Values

Once a column is a datetime, you can ask time-based questions.

  • What hour do orders spike?
  • Which quarter had the most signups?
  • Which weekday is cursed has fewer sales?

Pandas exposes date helpers through the .dt accessor, so you can pull out a Series of date parts directly:

df["signup_year"] = df["signup_at"].dt.year  # 2024
df["signup_month"] = df["signup_at"].dt.month  # 1 = January
df["signup_day"] = df["signup_at"].dt.day  # 15
df["signup_hour"] = df["signup_at"].dt.hour  # 0-23
df["signup_weekday"] = df["signup_at"].dt.dayofweek  # 0 = Monday
df["signup_quarter"] = df["signup_at"].dt.quarter  # 2 (April-June)
df["signup_day_name"] = df["signup_at"].dt.day_name()  # "Monday"

Assignment

SnackStack's analytics team is tired of eyeballing raw timestamps in their activity reports. They want a short, human-friendly label on every device reading, like "Monday, Q1 2024".

Complete the label_events function. It accepts a DataFrame with a datetime timestamp column and returns a copy with a single new event_label column – a string that stitches a few date parts together into your own custom format.

For each row, build the label as "<weekday>, Q<quarter> <year>". For example, a timestamp of 2024-01-15 10:30:00 (a Monday) becomes "Monday, Q1 2024".