0
0
Data Analysis Pythondata~5 mins

Extracting date components (year, month, day) in Data Analysis Python

Choose your learning style9 modes available
Introduction

We extract date components like year, month, and day from date columns to group, filter, and analyze time-based data more easily.

When you need to group sales or events by year or month.
When you want to filter data for a specific month or day.
When you need to create new columns for time-based analysis.
When you want to compare data across different time periods.
When building dashboards that show trends by year, quarter, or month.
Syntax
Data Analysis Python
df['column'].dt.year
df['column'].dt.month
df['column'].dt.day

The .dt accessor works only on datetime columns in pandas.

Use pd.to_datetime() first if the column is stored as a string.

Examples
Extracts the year from the date column and stores it in a new column called year.
Data Analysis Python
df['year'] = df['date'].dt.year
Extracts the month as a number (1-12) from the date column.
Data Analysis Python
df['month'] = df['date'].dt.month
Extracts the day of the month from the date column.
Data Analysis Python
df['day'] = df['date'].dt.day
Sample Program

This program creates a table of orders with dates and amounts. It converts the date strings to datetime objects using pd.to_datetime(), then extracts year, month, and day into separate columns using the .dt accessor. Finally, it groups orders by month and calculates total amounts per month.

Data Analysis Python
import pandas as pd

# Create sample data with dates
orders = pd.DataFrame({
    'order_date': ['2024-01-15', '2024-03-22', '2024-07-08', '2024-11-30'],
    'amount': [150, 200, 320, 180]
})

# Convert string to datetime
orders['order_date'] = pd.to_datetime(orders['order_date'])

# Extract date components
orders['year'] = orders['order_date'].dt.year
orders['month'] = orders['order_date'].dt.month
orders['day'] = orders['order_date'].dt.day

print(orders)

# Group by month and sum amounts
monthly_totals = orders.groupby('month')['amount'].sum()
print('\nMonthly totals:')
print(monthly_totals)
OutputSuccess
Important Notes

Always convert date columns to datetime type before using .dt accessor.

Use .dt.month_name() to get month names like January, February instead of numbers.

Use .dt.day_name() to get weekday names like Monday, Tuesday.

Summary

Use .dt.year, .dt.month, and .dt.day to extract date parts.

Convert strings to datetime first with pd.to_datetime().

Extracted components are useful for grouping, filtering, and time-based analysis.