0
0
Data Analysis Pythondata~20 mins

Extracting date components (year, month, day) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Component Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Extract year from a pandas datetime column
Given the DataFrame df with a datetime column date, what is the output of df['date'].dt.year?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2022-12-31', '2024-06-01'])})
result = df['date'].dt.year
print(result.tolist())
A[1, 12, 6]
B[15, 31, 1]
C[2023-01-15, 2022-12-31, 2024-06-01]
D[2023, 2022, 2024]
Attempts:
2 left
💡 Hint
Use the dt accessor to get date parts from pandas datetime columns.
data_output
intermediate
2:00remaining
Extract month and day from datetime
What is the output of extracting month and day from the datetime column df['date'] using df['date'].dt.month and df['date'].dt.day?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2022-12-31', '2024-06-01'])})
months = df['date'].dt.month
days = df['date'].dt.day
print(months.tolist(), days.tolist())
A[1, 12, 6] [15, 31, 1]
B[15, 31, 1] [1, 12, 6]
C[2023, 2022, 2024] [15, 31, 1]
D[1, 12, 6] [2023, 2022, 2024]
Attempts:
2 left
💡 Hint
Month and day are separate attributes accessed via dt.month and dt.day.
🔧 Debug
advanced
2:00remaining
Identify the error when extracting date components
What error will this code raise?
import pandas as pd

df = pd.DataFrame({'date': ['2023-01-15', '2022-12-31', '2024-06-01']})
years = df['date'].dt.year
print(years.tolist())
AAttributeError: Can only use .dt accessor with datetimelike values
BKeyError: 'year'
CTypeError: 'list' object is not callable
DNo error, prints [2023, 2022, 2024]
Attempts:
2 left
💡 Hint
Check the data type of the 'date' column before using .dt.
🚀 Application
advanced
2:30remaining
Create new columns for year, month, and day
Given a DataFrame df with a datetime column date, which code correctly adds three new columns year, month, and day with the respective date parts?
Adf[['year', 'month', 'day']] = df['date'].dt.year, df['date'].dt.month, df['date'].dt.day
Bdf['year'], df['month'], df['day'] = df['date'].dt.year, df['date'].dt.month, df['date'].dt.day
C
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
D
df['year'] = df['date'].year
df['month'] = df['date'].month
df['day'] = df['date'].day
Attempts:
2 left
💡 Hint
Use the dt accessor and assign each part to a separate column.
🧠 Conceptual
expert
2:30remaining
Understanding datetime component extraction behavior
If a pandas Series contains some missing datetime values (NaT), what will be the output of extracting the year component using series.dt.year?
AA Series of strings with years and 'NaT' for missing values
BA float Series with years and NaN for missing values
CAn integer Series with years and 0 for missing values
DRaises an error because of missing values
Attempts:
2 left
💡 Hint
Consider how pandas handles missing datetime values in numeric extraction.