Challenge - 5 Problems
Datetime Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Extract year from a pandas datetime column
What is the output of this code snippet that extracts the year from a datetime column in a pandas DataFrame?
Pandas
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2022-12-31', '2024-06-01'])}) df['year'] = df['date'].dt.year print(df['year'].tolist())
Attempts:
2 left
💡 Hint
Use the .dt accessor to get the year from datetime values.
✗ Incorrect
The .dt.year extracts the year part from each datetime value in the column, resulting in a list of years.
❓ Predict Output
intermediate2:00remaining
Extract month from a pandas datetime column
What will be printed by this code that extracts the month from a pandas datetime column?
Pandas
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2022-12-31', '2024-06-01'])}) df['month'] = df['date'].dt.month print(df['month'].tolist())
Attempts:
2 left
💡 Hint
The .dt.month extracts the month number from datetime values.
✗ Incorrect
The .dt.month extracts the month part from each datetime value, resulting in a list of month numbers.
❓ Predict Output
advanced2:00remaining
Extract day from a pandas datetime column
What is the output of this code that extracts the day from a datetime column in pandas?
Pandas
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2022-12-31', '2024-06-01'])}) df['day'] = df['date'].dt.day print(df['day'].tolist())
Attempts:
2 left
💡 Hint
Use .dt.day to get the day number from datetime values.
✗ Incorrect
The .dt.day extracts the day part from each datetime value, resulting in a list of day numbers.
❓ data_output
advanced2:00remaining
Count unique years in a datetime column
Given a pandas DataFrame with a datetime column, how many unique years are in the 'date' column after extracting the year?
Pandas
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2022-12-31', '2024-06-01', '2023-07-20'])}) df['year'] = df['date'].dt.year unique_years = df['year'].nunique() print(unique_years)
Attempts:
2 left
💡 Hint
Use .nunique() to count unique values in a column.
✗ Incorrect
The years extracted are 2023, 2022, 2024, and 2023 again. Unique years are 2023, 2022, and 2024, so count is 3.
🧠 Conceptual
expert2:00remaining
Understanding datetime extraction behavior with missing values
What happens when you extract year, month, or day from a pandas datetime column that contains missing values (NaT)?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', None, '2024-06-01'])}) df['year'] = df['date'].dt.year print(df['year'].tolist())
Attempts:
2 left
💡 Hint
Missing datetime values become pandas in extracted integer columns.
✗ Incorrect
When extracting year from a datetime column with missing values, pandas returns (pandas' missing integer type) for those rows, not nan or None.