The dt accessor allows you to access datetime properties and methods of pandas Series with datetime values, making it easy to extract parts like year, month, day, and more.
Click to reveal answer
beginner
How do you extract the year from a pandas Series of datetime values?
Use series.dt.year to get the year part of each datetime in the Series.
Click to reveal answer
intermediate
Which dt property would you use to get the day of the week as an integer (Monday=0)?
Use series.dt.dayofweek to get the day of the week as an integer where Monday is 0 and Sunday is 6.
Click to reveal answer
beginner
True or False: You can use dt accessor to convert datetime Series to strings.
True. Use series.dt.strftime() to format datetime as strings with the dt accessor.
Click to reveal answer
beginner
What will series.dt.hour return when applied to a datetime Series?
It returns the hour part (0 to 23) of each datetime value in the Series.
Click to reveal answer
Which pandas dt property gives the month number from a datetime Series?
Aseries.dt.month
Bseries.dt.day
Cseries.dt.year
Dseries.dt.hour
✗ Incorrect
The dt.month property extracts the month number (1 to 12) from each datetime value.
What does series.dt.dayofyear return?
ADay of the year (1 to 365 or 366)
BDay of the week as an integer
CDay of the month
DWeek number of the year
✗ Incorrect
dt.dayofyear returns the day number within the year, starting at 1.
If you want to get the weekday name like 'Monday' from a datetime Series, which dt method do you use?
Aseries.dt.weekday
Bseries.dt.day_name()
Cseries.dt.dayofweek
Dseries.dt.weekday_name
✗ Incorrect
Use series.dt.day_name() to get the weekday name as a string.
Which of these is NOT a valid dt property in pandas?
Aseries.dt.minute
Bseries.dt.second
Cseries.dt.month_name()
Dseries.dt.hour_of_day
✗ Incorrect
dt.hour_of_day is not a valid property. Use dt.hour instead.
What type of pandas object must you have to use the dt accessor?
ASeries with string values
BDataFrame with any dtype
CSeries with datetime64 dtype
DList of datetime objects
✗ Incorrect
The dt accessor works only on Series with datetime64 dtype.
Explain how to extract the year, month, and day from a pandas datetime Series using the dt accessor.
Think about the properties that give parts of a date.
You got /4 concepts.
Describe how you would find the weekday name and the hour of the day from a datetime Series in pandas.
One returns a string name, the other returns a number.
You got /4 concepts.
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
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.
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.
Final Answer:
Extract parts like year, month, and day from datetime columns -> Option A
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
Step 1: Recall correct dt accessor syntax
To access datetime parts, use .dt followed by the property without parentheses.
The code creates a Series of datetime objects for three dates, then extracts the month number using dt.month.
Step 2: Determine the month numbers
January is month 1, July is 7, December is 12, so the list is [1, 7, 12].
Final Answer:
[1, 7, 12] -> Option C
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
Step 1: Check the data type of the Series
The Series contains strings, not datetime objects, so dt accessor cannot be used directly.
Step 2: Identify the error cause
Without converting to datetime using pd.to_datetime(), accessing dt.day raises an error.
Final Answer:
The Series is not converted to datetime before using dt accessor -> Option A
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
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).
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.
Final Answer:
df['is_weekend'] = df['timestamp'].dt.weekday > 4 -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.