Bird
Raised Fist0
Pandasdata~20 mins

Extracting year, month, day in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Datetime Extraction 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
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())
A[2023, 2022, 2024]
B[15, 31, 1]
C[1, 12, 6]
D[2023-01-15, 2022-12-31, 2024-06-01]
Attempts:
2 left
💡 Hint
Use the .dt accessor to get the year from datetime values.
❓ Predict Output
intermediate
2: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())
A[15, 31, 1]
B[2023-01-15, 2022-12-31, 2024-06-01]
C[2023, 2022, 2024]
D[1, 12, 6]
Attempts:
2 left
💡 Hint
The .dt.month extracts the month number from datetime values.
❓ Predict Output
advanced
2: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())
A[2023-01-15, 2022-12-31, 2024-06-01]
B[15, 31, 1]
C[1, 12, 6]
D[2023, 2022, 2024]
Attempts:
2 left
💡 Hint
Use .dt.day to get the day number from datetime values.
❓ data_output
advanced
2: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)
A4
B1
C3
D2
Attempts:
2 left
💡 Hint
Use .nunique() to count unique values in a column.
🧠 Conceptual
expert
2: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())
A[2023, <NA>, 2024]
B[2023, None, 2024]
C[2023, NaT, 2024]
D[2023, nan, 2024]
Attempts:
2 left
💡 Hint
Missing datetime values become pandas in extracted integer columns.

Practice

(1/5)
1. What does the .dt.year attribute do when applied to a pandas datetime column?
easy
A. Extracts the year part from each date in the column
B. Extracts the month part from each date in the column
C. Extracts the day part from each date in the column
D. Converts the column to string format

Solution

  1. Step 1: Understand pandas datetime attributes

    The .dt accessor allows extraction of date parts like year, month, and day from datetime columns.
  2. Step 2: Identify the function of .dt.year

    The .dt.year extracts the year component from each datetime value in the column.
  3. Final Answer:

    Extracts the year part from each date in the column -> Option A
  4. Quick Check:

    .dt.year = year extraction [OK]
Hint: Remember: year, month, day are .dt.year, .dt.month, .dt.day [OK]
Common Mistakes:
  • Confusing .dt.year with .dt.month or .dt.day
  • Trying to use .year directly without .dt
  • Not converting column to datetime before extraction
2. Which of the following is the correct syntax to extract the month from a pandas datetime column named date_col?
easy
A. date_col.dt.month
B. date_col.month
C. date_col['month']
D. date_col.get_month()

Solution

  1. Step 1: Recall pandas datetime accessor usage

    To extract parts like month, you must use the .dt accessor on a datetime column.
  2. Step 2: Identify correct syntax for month extraction

    The correct syntax is date_col.dt.month. Other options either miss .dt or use invalid methods.
  3. Final Answer:

    date_col.dt.month -> Option A
  4. Quick Check:

    Use .dt before date parts [OK]
Hint: Always use .dt before year/month/day on datetime columns [OK]
Common Mistakes:
  • Using .month directly without .dt
  • Trying to access month as a dictionary key
  • Using non-existent methods like get_month()
3. Given the following code, what will be the output?
import pandas as pd
df = pd.DataFrame({
  'date': pd.to_datetime(['2023-01-15', '2022-12-31', '2024-07-04'])
})
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
df[['year', 'month', 'day']].values.tolist()
medium
A. [[15, 1, 2023], [31, 12, 2022], [4, 7, 2024]]
B. [['2023', '01', '15'], ['2022', '12', '31'], ['2024', '07', '04']]
C. [[2023, 1, 15], [2022, 12, 31], [2024, 7, 4]]
D. Error: Cannot extract year, month, day without conversion

Solution

  1. Step 1: Convert strings to datetime

    The pd.to_datetime converts string dates to datetime objects correctly.
  2. Step 2: Extract year, month, day

    Using .dt.year, .dt.month, and .dt.day extracts integer parts of the date.
  3. Final Answer:

    [[2023, 1, 15], [2022, 12, 31], [2024, 7, 4]] -> Option C
  4. Quick Check:

    Extracted parts are integers, not strings [OK]
Hint: Extracted year/month/day are integers, not strings [OK]
Common Mistakes:
  • Expecting strings instead of integers
  • Mixing order of day, month, year
  • Forgetting to convert to datetime first
4. What is wrong with this code snippet?
df['year'] = df['date'].year
Assuming df['date'] is a datetime column.
medium
A. You need to use df.year() instead
B. The column must be converted to string first
C. The code will work fine as is
D. It should be df['date'].dt.year to access year

Solution

  1. Step 1: Understand pandas datetime access

    Datetime columns require the .dt accessor to extract parts like year.
  2. Step 2: Identify missing .dt accessor

    The code misses .dt, so df['date'].year will raise an error or not work as expected.
  3. Final Answer:

    It should be df['date'].dt.year to access year -> Option D
  4. Quick Check:

    Use .dt before year/month/day [OK]
Hint: Always use .dt before year/month/day on datetime columns [OK]
Common Mistakes:
  • Omitting .dt accessor
  • Trying to call year() as a method
  • Converting datetime to string unnecessarily
5. You have a DataFrame df with a column 'timestamp' containing mixed string dates and some missing values (NaN). You want to create three new columns: 'year', 'month', and 'day'. Which approach correctly handles conversion and extraction without errors?
hard
A. df['year'] = df['timestamp'].dt.year; df['month'] = df['timestamp'].dt.month; df['day'] = df['timestamp'].dt.day
B. df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce'); df['year'] = df['timestamp'].dt.year; df['month'] = df['timestamp'].dt.month; df['day'] = df['timestamp'].dt.day
C. df['year'] = df['timestamp'].str[:4]; df['month'] = df['timestamp'].str[5:7]; df['day'] = df['timestamp'].str[8:10]
D. df['timestamp'] = df['timestamp'].astype(str); df['year'] = df['timestamp'].dt.year

Solution

  1. Step 1: Convert mixed strings and NaN to datetime safely

    Using pd.to_datetime(..., errors='coerce') converts valid strings to datetime and invalid/missing to NaT without error.
  2. Step 2: Extract year, month, day using .dt accessor

    After conversion, use .dt.year, .dt.month, and .dt.day to get parts safely, even with NaT values.
  3. Final Answer:

    df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce'); df['year'] = df['timestamp'].dt.year; df['month'] = df['timestamp'].dt.month; df['day'] = df['timestamp'].dt.day -> Option B
  4. Quick Check:

    Convert with errors='coerce' then extract parts [OK]
Hint: Use pd.to_datetime with errors='coerce' before extracting [OK]
Common Mistakes:
  • Not converting strings to datetime first
  • Extracting parts from string columns directly
  • Ignoring missing values causing errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard