We extract date components like year, month, and day from date columns to group, filter, and analyze time-based data more easily.
Extracting date components (year, month, day) in 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.
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
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.
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)
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.
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.