Challenge - 5 Problems
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 pandas datetime conversion?
Given the following code, what will be the output of
df['date_converted'].dtype?Pandas
import pandas as pd df = pd.DataFrame({'date_str': ['2023-01-01', '2023-06-15', '2023-12-31']}) df['date_converted'] = pd.to_datetime(df['date_str']) print(df['date_converted'].dtype)
Attempts:
2 left
💡 Hint
Think about what type pandas uses to store datetime values internally.
✗ Incorrect
When pandas converts strings to datetime, it uses the numpy datetime64[ns] type internally for efficient storage and operations.
❓ data_output
intermediate2:00remaining
What is the output of extracting the year from datetime?
What will be the output of
df['year'] after running this code?Pandas
import pandas as pd df = pd.DataFrame({'dates': pd.to_datetime(['2022-05-20', '2023-07-15', '2024-01-01'])}) df['year'] = df['dates'].dt.year print(df['year'].tolist())
Attempts:
2 left
💡 Hint
The
dt.year extracts the year as an integer from datetime values.✗ Incorrect
The
dt.year attribute extracts the year part as integers from datetime objects.🔧 Debug
advanced2:00remaining
Why does this code raise an error?
This code tries to convert a column to datetime but raises an error. What is the cause?
Pandas
import pandas as pd df = pd.DataFrame({'dates': ['2023-01-01', 'not_a_date', '2023-03-01']}) df['dates_converted'] = pd.to_datetime(df['dates'])
Attempts:
2 left
💡 Hint
Check if all strings in the column are valid date formats.
✗ Incorrect
The string 'not_a_date' cannot be parsed as a date, so pd.to_datetime raises a ValueError.
❓ visualization
advanced3:00remaining
Which plot shows the count of dates by month correctly?
Given this DataFrame, which option produces a bar plot showing counts of dates grouped by month?
Pandas
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'dates': pd.to_datetime(['2023-01-15', '2023-01-20', '2023-02-10', '2023-02-15', '2023-03-01'])})
Attempts:
2 left
💡 Hint
Use
dt.month and count values grouped by month, then plot a bar chart.✗ Incorrect
Option D correctly extracts month, counts occurrences sorted by month, and plots a bar chart.
🧠 Conceptual
expert3:00remaining
What is the effect of timezone conversion on pandas datetime?
If a pandas datetime column is timezone-naive and you convert it to a timezone-aware datetime with
dt.tz_localize('UTC'), what happens?Attempts:
2 left
💡 Hint
Localizing adds timezone info without changing the actual time values.
✗ Incorrect
Localizing a naive datetime assigns a timezone without changing the clock time, marking them as UTC times.