0
0
Data Analysis Pythondata~10 mins

Date feature extraction in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date feature extraction
Start with Date Column
Extract Year
Extract Month
Extract Day
Extract Weekday
Add Features to DataFrame
End
We start with a date column, then extract year, month, day, and weekday features step-by-step, adding them to the data.
Execution Sample
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2024-01-01', '2024-06-15', '2024-12-31'])})
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
df['weekday'] = df['date'].dt.day_name()
This code creates a DataFrame with dates and extracts year, month, day, and weekday names as new columns.
Execution Table
StepActionDate ValueExtracted YearExtracted MonthExtracted DayExtracted WeekdayDataFrame State
1Start with date '2024-01-01'2024-01-01{'date': '2024-01-01'}
2Extract year2024-01-012024{'date': '2024-01-01', 'year': 2024}
3Extract month2024-01-0120241{'date': '2024-01-01', 'year': 2024, 'month': 1}
4Extract day2024-01-01202411{'date': '2024-01-01', 'year': 2024, 'month': 1, 'day': 1}
5Extract weekday2024-01-01202411Monday{'date': '2024-01-01', 'year': 2024, 'month': 1, 'day': 1, 'weekday': 'Monday'}
6Repeat for '2024-06-15'2024-06-152024615Saturday{...second row added with extracted features...}
7Repeat for '2024-12-31'2024-12-3120241231Tuesday{...third row added with extracted features...}
8Final DataFrame with all features{'date': [...], 'year': [...], 'month': [...], 'day': [...], 'weekday': [...]}
💡 All dates processed; features extracted and added to DataFrame.
Variable Tracker
VariableStartAfter 1After 2After 3Final
df['date']empty2024-01-012024-06-152024-12-31[2024-01-01, 2024-06-15, 2024-12-31]
df['year']empty202420242024[2024, 2024, 2024]
df['month']empty1612[1, 6, 12]
df['day']empty11531[1, 15, 31]
df['weekday']emptyMondaySaturdayTuesday[Monday, Saturday, Tuesday]
Key Moments - 3 Insights
Why do we use 'dt' before year, month, day, and weekday?
The 'dt' accessor tells pandas we want to extract parts of a datetime object. Without 'dt', pandas can't access date features. See execution_table steps 2-5.
What happens if the date column is not in datetime format?
Extraction will fail or give errors because pandas needs datetime type to extract features. We convert with pd.to_datetime first (see execution_sample line 3).
Why is weekday extracted as a name, not a number?
Using 'day_name()' gives a friendly weekday name like 'Monday'. You can also use 'dt.weekday' for numbers 0-6. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the variable_tracker table, what is the value of df['month'] after processing the second date?
A[1, 6, 12]
B6
C[1, 6]
D15
💡 Hint
Check the 'df["month"]' row and the 'After 2' column in variable_tracker.
According to the execution_table, what weekday is extracted for the date '2024-12-31'?
ATuesday
BSaturday
CMonday
DSunday
💡 Hint
Look at step 7 in execution_table under 'Extracted Weekday'.
If the date column was not converted to datetime first, what would happen?
AExtraction works normally
BOnly year is extracted
CExtraction fails or errors occur
DWeekday is extracted as number
💡 Hint
Refer to key_moments question about datetime conversion.
Concept Snapshot
Date feature extraction:
- Use pandas 'dt' accessor on datetime column
- Extract year, month, day, weekday easily
- Convert column to datetime first with pd.to_datetime
- Add extracted features as new DataFrame columns
- Useful for time-based analysis and modeling
Full Transcript
This visual execution shows how to extract features from a date column in pandas. We start with a DataFrame containing dates. Using the 'dt' accessor, we extract year, month, day, and weekday name from each date. These features are added as new columns to the DataFrame. The execution table traces each step for each date, showing how the DataFrame grows. The variable tracker shows how each column changes after processing each date. Key moments clarify why the 'dt' accessor is needed and why datetime conversion is important. The quiz tests understanding of the extracted values and common pitfalls. This method helps turn raw dates into useful features for data analysis.