Challenge - 5 Problems
to_datetime Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this to_datetime conversion?
Given the following code, what will be the output of
print(df['date'])?Data Analysis Python
import pandas as pd df = pd.DataFrame({'date': ['2023-01-15', '2023/02/20', 'March 3, 2023']}) df['date'] = pd.to_datetime(df['date']) print(df['date'])
Attempts:
2 left
💡 Hint
The default time added by to_datetime when only date is given is midnight with seconds.
✗ Incorrect
Pandas to_datetime converts strings to Timestamp objects with time set to 00:00:00 by default if no time is specified.
❓ data_output
intermediate1:30remaining
How many rows have valid datetime after conversion?
Consider this DataFrame and conversion. How many rows have valid datetime (not NaT) after conversion?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'date': ['2023-01-01', 'invalid_date', '2023-03-15']}) df['date'] = pd.to_datetime(df['date'], errors='coerce') valid_count = df['date'].notna().sum() print(valid_count)
Attempts:
2 left
💡 Hint
Invalid dates become NaT with errors='coerce'.
✗ Incorrect
Only two strings are valid dates; the invalid one becomes NaT and is excluded from count.
🔧 Debug
advanced2:00remaining
What error does this to_datetime code raise?
What error will this code raise when run?
Data Analysis Python
import pandas as pd dates = ['2023-01-01', '2023-02-30'] df = pd.DataFrame({'date': dates}) df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d', errors='raise')
Attempts:
2 left
💡 Hint
February 30th is not a valid date.
✗ Incorrect
With errors='raise', invalid dates cause ValueError during conversion.
🚀 Application
advanced2:30remaining
Which option converts mixed date formats correctly?
You have a column with dates in formats like '2023-01-01', '01/02/2023', and 'March 3, 2023'. Which code correctly converts all to datetime?
Attempts:
2 left
💡 Hint
Use infer_datetime_format to handle multiple formats.
✗ Incorrect
Only option A uses infer_datetime_format=True and errors='coerce' to handle mixed formats safely.
🧠 Conceptual
expert1:30remaining
What is the dtype of a Series after to_datetime conversion with utc=True?
If you convert a Series of date strings using
pd.to_datetime(series, utc=True), what is the dtype of the resulting Series?Attempts:
2 left
💡 Hint
UTC conversion adds timezone info to dtype.
✗ Incorrect
Using utc=True converts dates to timezone-aware datetime64 with UTC timezone.