0
0
Data Analysis Pythondata~20 mins

to_datetime() conversion in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
to_datetime Mastery
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 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'])
A
0   2023-01-15 00:00:00
1   2023-02-20 00:00:00
2   2023-03-03 00:00:00
Name: date, dtype: datetime64[ns]
B
0   2023-01-15
1   2023-02-20
2   2023-03-03
Name: date, dtype: datetime64[ns]
C
0   2023-01-15 00:00
1   2023-02-20 00:00
2   2023-03-03 00:00
Name: date, dtype: datetime64[ns]
D
0   2023-01-15T00:00:00
1   2023-02-20T00:00:00
2   2023-03-03T00:00:00
Name: date, dtype: datetime64[ns]
Attempts:
2 left
💡 Hint
The default time added by to_datetime when only date is given is midnight with seconds.
data_output
intermediate
1: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)
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Invalid dates become NaT with errors='coerce'.
🔧 Debug
advanced
2: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')
AKeyError: 'date'
BValueError: day is out of range for month
CTypeError: unsupported operand type(s)
DNo error, conversion succeeds
Attempts:
2 left
💡 Hint
February 30th is not a valid date.
🚀 Application
advanced
2: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?
Apd.to_datetime(df['date'], infer_datetime_format=True, errors='coerce')
Bpd.to_datetime(df['date'], format='%m/%d/%Y')
Cpd.to_datetime(df['date'], format='%B %d, %Y')
Dpd.to_datetime(df['date'], format='%Y-%m-%d')
Attempts:
2 left
💡 Hint
Use infer_datetime_format to handle multiple formats.
🧠 Conceptual
expert
1: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?
Astring
Bdatetime64[ns]
Cobject
Ddatetime64[ns, UTC]
Attempts:
2 left
💡 Hint
UTC conversion adds timezone info to dtype.