Challenge - 5 Problems
Date Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this date parsing code?
Given the following pandas code, what is the output of the
print(dates) statement?Pandas
import pandas as pd dates = pd.to_datetime(['2023-01-15', '2023/02/20', 'March 3, 2023']) print(dates)
Attempts:
2 left
💡 Hint
Look at how pandas converts strings to Timestamps with default time 00:00:00.
✗ Incorrect
pandas.to_datetime converts strings to Timestamp objects with time set to midnight by default. The output shows a DatetimeIndex with Timestamps including time 00:00:00.
❓ data_output
intermediate1:30remaining
How many valid dates are parsed?
Using
pd.to_datetime with errors='coerce', how many valid dates remain after parsing?Pandas
import pandas as pd dates = pd.to_datetime(['2023-01-01', 'not a date', '2023-03-15'], errors='coerce') valid_count = dates.notna().sum() print(valid_count)
Attempts:
2 left
💡 Hint
Check how 'not a date' is handled with errors='coerce'.
✗ Incorrect
The invalid string is converted to NaT (missing date). Only two valid dates remain, so the count is 2.
🔧 Debug
advanced2:00remaining
Why does this code raise an error?
This code raises an error. What is the cause?
Pandas
import pandas as pd dates = ['2023-01-01', '2023-02-30'] pd.to_datetime(dates, format='%Y-%m-%d')
Attempts:
2 left
💡 Hint
Check if February 30 exists in the calendar.
✗ Incorrect
February 30 does not exist, so pandas raises a ValueError when parsing with a strict format.
🚀 Application
advanced2:00remaining
Convert mixed-format dates to datetime with dayfirst
You have dates in mixed formats like '31/12/2023' and '2023-12-31'. Which code correctly parses them assuming day comes first?
Attempts:
2 left
💡 Hint
Use dayfirst=True to handle day/month order in mixed formats.
✗ Incorrect
dayfirst=True tells pandas to interpret the first number as day when ambiguous. This works for mixed formats without specifying a fixed format.
🧠 Conceptual
expert1:30remaining
What does the
utc=True parameter do in pd.to_datetime()?Select the correct explanation of the effect of
utc=True when parsing dates with pd.to_datetime().Attempts:
2 left
💡 Hint
Think about timezone awareness in pandas datetime objects.
✗ Incorrect
Setting utc=True converts all parsed dates to UTC timezone, making them timezone-aware Timestamps.