Bird
Raised Fist0
Pandasdata~20 mins

dt accessor for datetime properties in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Datetime Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of extracting month using dt accessor
What is the output of this code snippet?
Pandas
import pandas as pd
s = pd.Series(pd.to_datetime(['2023-01-15', '2023-06-20', '2023-12-31']))
result = s.dt.month
print(result.tolist())
A[15, 20, 31]
B[1, 6, 12]
C[2023, 2023, 2023]
D[0, 5, 11]
Attempts:
2 left
💡 Hint
The dt accessor extracts datetime components like year, month, day.
❓ data_output
intermediate
2:00remaining
Number of weekend days in a datetime Series
Given a pandas Series of dates, which option correctly counts how many dates fall on a weekend (Saturday or Sunday)?
Pandas
import pandas as pd
s = pd.Series(pd.to_datetime(['2023-04-01', '2023-04-02', '2023-04-03', '2023-04-04']))
weekends = s.dt.dayofweek >= 5
count = weekends.sum()
print(count)
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Dayofweek returns 0 for Monday and 6 for Sunday.
🔧 Debug
advanced
2:00remaining
Identify the error in using dt accessor
What error does this code raise?
Pandas
import pandas as pd
s = pd.Series(['2023-01-01', '2023-02-01', '2023-03-01'])
result = s.dt.day
print(result)
ANo error, prints day numbers
BTypeError: unsupported operand type(s) for -: 'str' and 'int'
CAttributeError: Can only use .dt accessor with datetimelike values
DKeyError: 'day'
Attempts:
2 left
💡 Hint
The dt accessor requires datetime type, not strings.
🚀 Application
advanced
2:00remaining
Filter rows by hour using dt accessor
You have a DataFrame with a datetime column 'timestamp'. Which code filters rows where the hour is between 9 AM and 5 PM (inclusive)?
Pandas
import pandas as pd
df = pd.DataFrame({'timestamp': pd.to_datetime(['2023-01-01 08:00', '2023-01-01 09:00', '2023-01-01 17:00', '2023-01-01 18:00'])})
Adf[(df['timestamp'].dt.hour >= 9) & (df['timestamp'].dt.hour <= 17)]
Bdf[(df['timestamp'].dt.hour > 9) & (df['timestamp'].dt.hour < 17)]
Cdf[(df['timestamp'].dt.hour >= 8) & (df['timestamp'].dt.hour <= 18)]
Ddf[(df['timestamp'].dt.hour == 9) | (df['timestamp'].dt.hour == 17)]
Attempts:
2 left
💡 Hint
Use inclusive comparison operators to include 9 AM and 5 PM.
🧠 Conceptual
expert
2:00remaining
Understanding dt accessor behavior with missing values
Given a Series with some missing datetime values, what is the output of accessing dt.day on it?
Pandas
import pandas as pd
s = pd.Series([pd.Timestamp('2023-01-01'), pd.NaT, pd.Timestamp('2023-01-03')])
result = s.dt.day
print(result.tolist())
A[1, <NA>, 3]
B[1, null, 3]
C[1, NaT, 3]
D[1, nan, 3]
Attempts:
2 left
💡 Hint
Missing datetime values become pandas NA in dt accessor outputs.

Practice

(1/5)
1.

What does the dt accessor in pandas allow you to do?

easy
A. Extract parts like year, month, and day from datetime columns
B. Convert strings to numbers
C. Sort data alphabetically
D. Create new columns with random values

Solution

  1. Step 1: Understand the purpose of dt accessor

    The dt accessor is used with datetime columns to access their components like year, month, day, etc.
  2. Step 2: Compare options with this purpose

    Only Extract parts like year, month, and day from datetime columns describes extracting parts of datetime, which matches the dt accessor's use.
  3. Final Answer:

    Extract parts like year, month, and day from datetime columns -> Option A
  4. Quick Check:

    dt accessor = extract datetime parts [OK]
Hint: Remember: dt means date-time parts extraction [OK]
Common Mistakes:
  • Thinking dt converts strings to numbers
  • Confusing dt with sorting functions
  • Assuming dt creates random data
2.

Which of the following is the correct syntax to get the month from a pandas datetime column df['date']?

df['date']?.month
easy
A. df['date'].month()
B. df['date'].dt.month
C. df['date'].dt('month')
D. df['date'].month.dt

Solution

  1. Step 1: Recall correct dt accessor syntax

    To access datetime parts, use .dt followed by the property without parentheses.
  2. Step 2: Check each option

    df['date'].dt.month uses .dt.month correctly. df['date'].month() wrongly uses parentheses. df['date'].dt('month') uses wrong syntax. df['date'].month.dt reverses order.
  3. Final Answer:

    df['date'].dt.month -> Option B
  4. Quick Check:

    Use .dt.property syntax [OK]
Hint: Use .dt before datetime property without parentheses [OK]
Common Mistakes:
  • Calling month as a function with ()
  • Using dt as a function
  • Reversing dt and property order
3.

Given the following code, what will be the output?

import pandas as pd

dates = pd.Series(pd.to_datetime(['2023-01-15', '2023-07-20', '2023-12-31']))
months = dates.dt.month
print(months.tolist())
medium
A. [15, 20, 31]
B. ['January', 'July', 'December']
C. [1, 7, 12]
D. [2023, 2023, 2023]

Solution

  1. Step 1: Understand the code

    The code creates a Series of datetime objects for three dates, then extracts the month number using dt.month.
  2. Step 2: Determine the month numbers

    January is month 1, July is 7, December is 12, so the list is [1, 7, 12].
  3. Final Answer:

    [1, 7, 12] -> Option C
  4. Quick Check:

    dt.month extracts month numbers [OK]
Hint: dt.month returns month numbers as integers [OK]
Common Mistakes:
  • Confusing day with month
  • Expecting month names instead of numbers
  • Printing year instead of month
4.

What is wrong with this code snippet?

import pandas as pd

dates = pd.Series(['2023-01-15', '2023-07-20'])
print(dates.dt.day)
medium
A. The Series is not converted to datetime before using dt accessor
B. The dt accessor does not have a day property
C. The print statement syntax is incorrect
D. The dates list is empty

Solution

  1. Step 1: Check the data type of the Series

    The Series contains strings, not datetime objects, so dt accessor cannot be used directly.
  2. Step 2: Identify the error cause

    Without converting to datetime using pd.to_datetime(), accessing dt.day raises an error.
  3. Final Answer:

    The Series is not converted to datetime before using dt accessor -> Option A
  4. Quick Check:

    Convert to datetime before using dt [OK]
Hint: Always convert to datetime before using dt accessor [OK]
Common Mistakes:
  • Assuming dt works on string Series
  • Thinking dt.day does not exist
  • Ignoring data type requirements
5.

You have a DataFrame df with a datetime column 'timestamp'. You want to create a new column 'is_weekend' that is True if the date is Saturday or Sunday, and False otherwise. Which code correctly does this?

df['is_weekend'] = df['timestamp'].dt.weekday > 4
hard
A. df['is_weekend'] = df['timestamp'].dt.weekday in [5, 6]
B. df['is_weekend'] = df['timestamp'].dt.day > 4
C. df['is_weekend'] = df['timestamp'].dt.weekday == 5 or 6
D. df['is_weekend'] = df['timestamp'].dt.weekday > 4

Solution

  1. Step 1: Understand weekday numbering

    In pandas, dt.weekday returns 0 for Monday up to 6 for Sunday. Weekend days are 5 (Saturday) and 6 (Sunday).
  2. Step 2: Check each option's logic

    df['is_weekend'] = df['timestamp'].dt.weekday > 4 uses > 4, which means days 5 and 6 are True. df['is_weekend'] = df['timestamp'].dt.day > 4 checks day of month, not weekday. df['is_weekend'] = df['timestamp'].dt.weekday == 5 or 6 uses incorrect syntax for or. df['is_weekend'] = df['timestamp'].dt.weekday in [5, 6] uses 'in' which is invalid for Series.
  3. Final Answer:

    df['is_weekend'] = df['timestamp'].dt.weekday > 4 -> Option D
  4. Quick Check:

    Weekend days have weekday > 4 [OK]
Hint: Weekend days have weekday numbers 5 or 6, so > 4 works [OK]
Common Mistakes:
  • Using day instead of weekday
  • Wrong boolean logic with or
  • Using 'in' with pandas Series
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