

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Pandas
incomplete
2: Data Types
incomplete
3: Extracting Data
incomplete
4: Dates and Times
incomplete
5: Datetime Math
incomplete
6: Comparing Dates
incomplete
7: List Comprehensions
incomplete
8: Dictionary Comprehensions
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Dates are deceptively tricky. They look simple, but they are not. Fortunately, Python has a datetime module built into the standard library, which offers tools for working with dates.
In real datasets, you'll find:
Different date formats
Missing time zones
Weird offsets
Inconsistent timestamps
Business logic like "last 30 days" or "next business week"
... and it's up to you to figure out how to handle it.
You'll often receive a timestamp as a string:
timestamp = "2026-05-28 14:30:00"Which you'll then convert to a datetime object using datetime.strptime():
from datetime import datetime
timestamp = datetime.strptime("2026-05-28 14:30:00", "%Y-%m-%d %H:%M:%S")
print(timestamp)
# 2026-05-28 14:30:00The timestamp here is an actual datetime object, not just a string. You can do math with it, compare it to other dates, etc.
That format string, "%Y-%m-%d %H:%M:%S", tells Python how to parse the text as a date and time:
%Y = 4-digit year
%m = month
%d = day
%H = hour in 24-hour time
%M = minute
%S = second
Different sources will often hand you different stringified date and time formats:
iso_date = datetime.strptime("2024-01-15", "%Y-%m-%d")
us_date = datetime.strptime("01/15/2024", "%m/%d/%Y")
eu_date = datetime.strptime("15/01/2024", "%d/%m/%Y")If the format string doesn't match the text exactly, parsing fails, and that's why dates are such a common cleaning problem.
SnackStack's smart appliances send event timestamps as strings... guh.
Complete the parse_reading_timestamp function. It accepts a timestamp string and returns a datetime object.
Parse the timestamp with datetime.strptime() using the format string "%Y-%m-%d %H:%M:%S".
The incoming timestamp string looks like "2022-02-22 02:22:22".