0
0
Pandasdata~10 mins

Extracting year, month, day in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extracting year, month, day
Start with datetime column
Use .dt accessor
Extract year/month/day
Create new columns with extracted values
Use extracted data for analysis
We start with a datetime column, use pandas .dt accessor to get year, month, and day, then store these in new columns for easy use.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2024-06-20'])})
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
print(df)
This code creates a DataFrame with dates and extracts year, month, and day into new columns.
Execution Table
StepActionCodeResult
1Create DataFrame with date columndf = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2024-06-20'])})df with 'date' column: [2023-01-15, 2024-06-20]
2Extract year from 'date'df['year'] = df['date'].dt.yearyear column: [2023, 2024]
3Extract month from 'date'df['month'] = df['date'].dt.monthmonth column: [1, 6]
4Extract day from 'date'df['day'] = df['date'].dt.dayday column: [15, 20]
5Print final DataFrameprint(df)DataFrame: date year month day 0 2023-01-15 2023 1 15 1 2024-06-20 2024 6 20
💡 All date parts extracted and stored; execution ends after printing DataFrame.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
dfemptydate column with 2 datesyear column addedmonth column addedday column addedfinal DataFrame with date, year, month, day columns
Key Moments - 2 Insights
Why do we use .dt before year, month, or day?
The .dt accessor tells pandas we want to work with datetime properties of the column. Without .dt, pandas won't know to extract parts like year or month. See execution_table step 2.
Can we extract year, month, day directly without converting to datetime?
No, the column must be datetime type. pd.to_datetime converts strings to datetime. Without this, .dt.year etc. will cause errors. See execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'month' after step 3?
A[1, 6]
B[15, 20]
C[2023, 2024]
D['2023-01-15', '2024-06-20']
💡 Hint
Check the 'Result' column for step 3 in the execution_table.
At which step is the 'day' column added to the DataFrame?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action mentioning 'Extract day' in the execution_table.
If the 'date' column was not converted to datetime, what would happen when running step 2?
AIt would extract year correctly
BIt would raise an error
CIt would extract month instead
DIt would create empty columns
💡 Hint
Refer to key_moments about the need for datetime type before using .dt accessor.
Concept Snapshot
Extract year, month, day from datetime column in pandas:
- Ensure column is datetime type (use pd.to_datetime)
- Use .dt.year, .dt.month, .dt.day to get parts
- Assign to new columns for easy access
- Useful for time-based analysis and filtering
Full Transcript
We start with a pandas DataFrame containing a date column. First, we convert the date strings to datetime type using pd.to_datetime. Then, using the .dt accessor, we extract the year, month, and day parts from the datetime column. Each extracted part is stored in a new column in the DataFrame. Finally, we print the DataFrame showing the original date and the extracted year, month, and day columns. This process helps us work easily with parts of dates for analysis.