0
0
Pandasdata~20 mins

to_datetime() for date parsing in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[Timestamp('2023-01-15 00:00:00') Timestamp('2023-02-20 00:00:00') Timestamp('2023-03-03 00:00:00')]
B[Timestamp('2023-01-15') Timestamp('2023-02-20') Timestamp('2023-03-03')]
C[datetime.date(2023, 1, 15) datetime.date(2023, 2, 20) datetime.date(2023, 3, 3)]
DError: Invalid date format
Attempts:
2 left
💡 Hint
Look at how pandas converts strings to Timestamps with default time 00:00:00.
data_output
intermediate
1: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)
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Check how 'not a date' is handled with errors='coerce'.
🔧 Debug
advanced
2: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')
ANo error, returns NaT for invalid date
BValueError because '2023-02-30' is an invalid date
CSyntaxError due to missing parentheses
DTypeError because format string is incorrect
Attempts:
2 left
💡 Hint
Check if February 30 exists in the calendar.
🚀 Application
advanced
2: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?
Apd.to_datetime(['31/12/2023', '2023-12-31'], format='%Y-%m-%d')
Bpd.to_datetime(['31/12/2023', '2023-12-31'], format='%d/%m/%Y')
Cpd.to_datetime(['31/12/2023', '2023-12-31'], dayfirst=False)
Dpd.to_datetime(['31/12/2023', '2023-12-31'], dayfirst=True)
Attempts:
2 left
💡 Hint
Use dayfirst=True to handle day/month order in mixed formats.
🧠 Conceptual
expert
1: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().
AConverts parsed dates to UTC timezone, returning timezone-aware Timestamps
BRaises an error if timezone info is missing in input
CConverts all dates to local system timezone
DIgnores timezone information and returns naive datetime objects
Attempts:
2 left
💡 Hint
Think about timezone awareness in pandas datetime objects.