0
0
Data Analysis Pythondata~10 mins

Extracting date components (year, month, day) in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extracting date components (year, month, day)
Start with date column
Extract year from date
Extract month from date
Extract day from date
Store or display extracted components
We start with a date column, then extract year, month, and day one by one, and finally store or show these parts.
Execution Sample
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2024-06-01', '2023-12-25'])})
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
print(df)
This code creates a table with dates and adds columns for year, month, and day extracted from those dates.
Execution Table
StepActionInputOutput/Result
1Create DataFrame with date column['2024-06-01', '2023-12-25']DataFrame with 'date' column of datetime type
2Extract year from 'date'2024-06-01, 2023-12-25[2024, 2023] stored in 'year' column
3Extract month from 'date'2024-06-01, 2023-12-25[6, 12] stored in 'month' column
4Extract day from 'date'2024-06-01, 2023-12-25[1, 25] stored in 'day' column
5Print DataFrameDataFrame with 'date', 'year', 'month', 'day'Shows table with all columns and values
💡 All date components extracted and stored; DataFrame ready for use.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
dfNonedate column with ['2024-06-01', '2023-12-25']Added 'year' column [2024, 2023]Added 'month' column [6, 12]Added 'day' column [1, 25]DataFrame with 'date', 'year', 'month', 'day' columns
Key Moments - 2 Insights
Why do we use .dt before year, month, and day?
The .dt accessor tells pandas we want to work with datetime properties of the column. Without it, pandas won't know to extract date parts. See execution_table step 2-4.
What happens if the 'date' column is not in datetime format?
Extraction will fail or give errors because .dt only works on datetime types. You must convert strings to datetime first, as done in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, what values are stored in the 'month' column?
A[2024, 2023]
B[6, 12]
C[1, 25]
D['2024-06-01', '2023-12-25']
💡 Hint
Check the 'Output/Result' column for step 3 in the execution_table.
At which step does the DataFrame get the 'day' column added?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action 'Extract day from date' in the execution_table.
If the 'date' column was not converted to datetime, what would happen when extracting year?
AExtraction gives an error or wrong result
BYear column will be all zeros
CExtraction works normally
DMonth column will be extracted instead
💡 Hint
Refer to key_moments about the importance of datetime type for .dt accessor.
Concept Snapshot
Extract year, month, day from a datetime column using pandas:
Use df['date'].dt.year, .dt.month, .dt.day
Make sure 'date' is datetime type first
Adds new columns with extracted parts
Useful for time-based analysis
Full Transcript
We start with a DataFrame containing a date column. We convert the date strings to datetime type so pandas can understand them. Then, using the .dt accessor, we extract the year, month, and day parts from each date. These parts are stored in new columns in the DataFrame. Finally, we print the DataFrame to see all columns together. This process helps us break down dates into useful pieces for analysis.