0
0
Pandasdata~10 mins

dt accessor for datetime properties in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - dt accessor for datetime properties
Start with datetime Series
Use .dt accessor
Access datetime property
Get new Series with property values
Use result for analysis or display
You start with a pandas Series of dates, use the .dt accessor to reach datetime properties, then get a new Series with those property values.
Execution Sample
Pandas
import pandas as pd
s = pd.Series(pd.to_datetime(['2023-01-01', '2023-06-15', '2023-12-31']))
s_month = s.dt.month
print(s_month)
This code creates a Series of dates, extracts the month from each date using .dt.month, and prints the month numbers.
Execution Table
StepActionInputOutput
1Create Series s['2023-01-01', '2023-06-15', '2023-12-31']Datetime Series with 3 dates
2Access s.dt.monthDatetime SeriesSeries with values [1, 6, 12]
3Print s_monthSeries [1, 6, 12]Output: 0 1 1 6 2 12 dtype: int64
💡 All dates processed, months extracted for each date
Variable Tracker
VariableStartAfter Step 1After Step 2Final
sundefinedDatetime Series ['2023-01-01', '2023-06-15', '2023-12-31']SameSame
s_monthundefinedundefinedSeries [1, 6, 12]Same
Key Moments - 3 Insights
Why do we use .dt before accessing month?
The .dt accessor tells pandas we want to work with datetime properties. Without it, pandas doesn't know to treat the Series as dates. See execution_table step 2.
What type of data does s_month hold?
s_month is a Series of integers representing months extracted from each date. This is shown in execution_table step 2 output.
Can we use .dt on a Series that is not datetime?
No, .dt only works on Series with datetime dtype. Otherwise, pandas will raise an error. This is why step 1 converts strings to datetime.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2. What is the value of s_month at index 1?
A12
B1
C6
D15
💡 Hint
Check the Output column in step 2 showing the Series values [1, 6, 12]
At which step is the datetime Series created?
AStep 3
BStep 1
CStep 2
DBefore Step 1
💡 Hint
Look at the Action column in execution_table where Series s is created
If the Series s was not converted to datetime, what would happen when accessing s.dt.month?
AIt would raise an error
BIt would return the month numbers anyway
CIt would return None values
DIt would convert automatically
💡 Hint
Refer to key_moments about .dt usage only on datetime Series
Concept Snapshot
Use pandas .dt accessor to get datetime properties from a Series.
Example: s.dt.month returns month numbers.
Works only if Series dtype is datetime.
Returns a new Series with the property values.
Useful for extracting year, month, day, weekday, etc.
Full Transcript
This visual trace shows how to use pandas .dt accessor to get datetime properties. We start with a Series of date strings converted to datetime. Then we use s.dt.month to get the month number for each date. The execution table shows each step: creating the Series, accessing the month property, and printing the result. The variable tracker shows how s and s_month change. Key moments clarify why .dt is needed and what data type results. The quiz tests understanding of values and steps. The snapshot summarizes the concept simply.