Challenge - 5 Problems
DateTime Feature Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Extracting the hour from a datetime column
What is the output of this code that extracts the hour from a datetime column in a pandas DataFrame?
ML Python
import pandas as pd df = pd.DataFrame({'timestamp': pd.to_datetime(['2024-06-01 08:15:27', '2024-06-01 22:45:00'])}) df['hour'] = df['timestamp'].dt.hour print(df['hour'].tolist())
Attempts:
2 left
💡 Hint
The .dt accessor helps extract parts of datetime like hour, minute, or day.
✗ Incorrect
The .dt.hour extracts the hour part from each datetime entry. The first timestamp has hour 8, the second 22.
🧠 Conceptual
intermediate1:00remaining
Why extract date and time features?
Why do we extract features like day of week or month from datetime data in machine learning?
Attempts:
2 left
💡 Hint
Think about how time affects behavior or events in real life.
✗ Incorrect
Extracting features like day of week or month helps models find patterns related to time, such as weekend effects or seasonal trends.
❓ Metrics
advanced1:30remaining
Evaluating model with time-based features
You added 'day_of_week' and 'hour' features extracted from timestamps to your model. Which metric best shows if these features improved the model's ability to predict hourly sales?
Attempts:
2 left
💡 Hint
Think about how to fairly test time-related predictions.
✗ Incorrect
Using MSE on a test set split by time ensures the model generalizes to future unseen time periods, which is important for time-based features.
🔧 Debug
advanced1:30remaining
Fixing incorrect extraction of month from datetime
What error does this code raise when trying to extract the month from a datetime column?
ML Python
import pandas as pd df = pd.DataFrame({'date': ['2024-06-01', '2024-07-15']}) df['month'] = df['date'].dt.month print(df['month'])
Attempts:
2 left
💡 Hint
Check the data type of the 'date' column before using .dt accessor.
✗ Incorrect
The 'date' column is string type, not datetime. The .dt accessor only works on datetime types, so it raises AttributeError.
❓ Model Choice
expert2:00remaining
Choosing model type for datetime feature importance
You have extracted multiple datetime features (hour, day_of_week, month) for a sales forecasting task. Which model type is best to capture complex interactions between these features and other variables?
Attempts:
2 left
💡 Hint
Consider models that handle non-linear relationships and feature interactions well.
✗ Incorrect
Decision Trees and Random Forests can capture complex, non-linear interactions between datetime features and other variables without needing manual feature engineering.