Challenge - 5 Problems
Date Feature Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Extracting Month and Day from Dates
What is the output of the following code that extracts the month and day from a pandas datetime column?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2023-07-04', '2023-12-31'])}) df['month'] = df['date'].dt.month df['day'] = df['date'].dt.day print(df[['month', 'day']].to_dict(orient='list'))
Attempts:
2 left
💡 Hint
Look at how pandas datetime properties .dt.month and .dt.day extract parts of the date.
✗ Incorrect
The .dt accessor allows extraction of datetime components. Here, month and day are extracted correctly from each date.
❓ data_output
intermediate2:00remaining
Counting Weekdays in a Date Range
Given a date range, how many Mondays are there in the DataFrame after extracting the weekday?
Data Analysis Python
import pandas as pd dates = pd.date_range(start='2023-01-01', end='2023-01-31') df = pd.DataFrame({'date': dates}) df['weekday'] = df['date'].dt.day_name() monday_count = df[df['weekday'] == 'Monday'].shape[0] print(monday_count)
Attempts:
2 left
💡 Hint
Count how many Mondays fall between Jan 1 and Jan 31, 2023.
✗ Incorrect
January 2023 has Mondays on 2nd, 9th, 16th, 23rd, and 30th, totaling 5.
❓ visualization
advanced3:00remaining
Plotting Hourly Distribution of Events
Which option produces the correct bar plot showing counts of events per hour from a datetime column?
Data Analysis Python
import pandas as pd import matplotlib.pyplot as plt # Sample data timestamps = pd.to_datetime([ '2023-06-01 08:15', '2023-06-01 08:45', '2023-06-01 09:00', '2023-06-01 10:30', '2023-06-01 10:45', '2023-06-01 10:50' ]) df = pd.DataFrame({'timestamp': timestamps}) df['hour'] = df['timestamp'].dt.hour counts = df['hour'].value_counts().sort_index() plt.bar(counts.index, counts.values) plt.xlabel('Hour of Day') plt.ylabel('Event Count') plt.title('Events per Hour') plt.show()
Attempts:
2 left
💡 Hint
Look for a bar chart with counts grouped by hour.
✗ Incorrect
The code groups events by hour and plots a bar chart with counts 2 at 8, 1 at 9, and 3 at 10.
🔧 Debug
advanced2:00remaining
Identifying Error in Date Extraction Code
What error does the following code raise when trying to extract the week number from a datetime column?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-01', '2023-06-15'])}) df['week_num'] = df['date'].dt.week print(df)
Attempts:
2 left
💡 Hint
Check if .dt.week is a valid attribute in pandas datetime accessor.
✗ Incorrect
The .dt.week attribute was deprecated and removed; use .dt.isocalendar().week instead.
🚀 Application
expert3:00remaining
Calculating Age from Birthdate Column
Given a DataFrame with a 'birthdate' column, which option correctly calculates the age in years as of today?
Data Analysis Python
import pandas as pd from datetime import datetime df = pd.DataFrame({'birthdate': pd.to_datetime(['1990-05-15', '2000-12-01', '1985-07-30'])}) # Calculate age here print(df['age'].tolist())
Attempts:
2 left
💡 Hint
Use pandas datetime subtraction and .dt.days to get days difference.
✗ Incorrect
Option A correctly uses pandas Timestamp subtraction and .dt.days to get days difference, then divides by 365 for age.