Challenge - 5 Problems
Datetime Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
What is the output of this datetime conversion?
Consider this pandas code that converts a string column to datetime. What will be the output of the
df['date'].dtype?Pandas
import pandas as pd df = pd.DataFrame({'date': ['2023-01-01', '2023-02-15', '2023-03-30']}) df['date'] = pd.to_datetime(df['date']) print(df['date'].dtype)
Attempts:
2 left
💡 Hint
Think about what pandas does when converting strings to datetime format.
✗ Incorrect
When pandas converts a string column to datetime using pd.to_datetime, the dtype changes from object (string) to datetime64[ns], which is pandas' internal datetime type.
❓ data_output
intermediate1:30remaining
How many rows are in this filtered datetime DataFrame?
Given this DataFrame with datetime values, how many rows remain after filtering for dates in March 2023?
Pandas
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-10', '2023-03-05', '2023-03-20', '2023-04-01'])}) filtered = df[(df['date'] >= '2023-03-01') & (df['date'] < '2023-04-01')] print(len(filtered))
Attempts:
2 left
💡 Hint
Count only dates that are in March 2023.
✗ Incorrect
Only the dates '2023-03-05' and '2023-03-20' fall within March 2023, so the filtered DataFrame has 2 rows.
🔧 Debug
advanced2:00remaining
Why does this code raise an error when subtracting dates?
This code tries to subtract two date columns but raises an error. What is the cause?
Pandas
import pandas as pd df = pd.DataFrame({'start': ['2023-01-01', '2023-02-01'], 'end': ['2023-01-10', '2023-02-15']}) df['duration'] = df['end'] - df['start'] print(df['duration'])
Attempts:
2 left
💡 Hint
Check the data types of the 'start' and 'end' columns before subtraction.
✗ Incorrect
The columns 'start' and 'end' are strings, so subtracting them raises a TypeError. They must be converted to datetime first.
❓ visualization
advanced2:30remaining
Which plot shows correct monthly sales trend?
Given monthly sales data with datetime index, which plot code correctly shows the sales trend over time?
Pandas
import pandas as pd import matplotlib.pyplot as plt sales = pd.Series([100, 150, 200, 250], index=pd.to_datetime(['2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01']))
Attempts:
2 left
💡 Hint
Think about which plot type best shows a trend over time.
✗ Incorrect
Using sales.plot() creates a line plot with datetime index on x-axis, showing the trend clearly. Bar or scatter plots are less suitable here.
🧠 Conceptual
expert2:00remaining
Why is timezone handling important in datetime data?
Which statement best explains why handling timezones correctly is crucial in datetime data analysis?
Attempts:
2 left
💡 Hint
Think about what happens if you compare times from different zones without adjustment.
✗ Incorrect
Ignoring timezones can lead to wrong conclusions when comparing or merging datetime data from different regions, causing errors in analysis.