📅 Working with Dates

Working with dates and times is essential for data analysis! Whether you're analyzing sales trends over time, tracking customer behavior, or creating time-based reports, pandas provides powerful tools for handling temporal data.

Think of dates like timestamps that help you organize events in chronological order and discover patterns over time.

import pandas as pd
from datetime import datetime

# Sales data with dates as strings
sales = pd.DataFrame({
    'date': ['2023-01-15', '2023-02-20', '2023-03-10'],
    'product': ['Laptop', 'Mouse', 'Keyboard'],
    'sales': [10, 25, 15]
})

print("Original data with date strings:")
print(sales)
print("Date column type:", sales['date'].dtype)
print()

# Convert to datetime
sales['date'] = pd.to_datetime(sales['date'])
print("After converting to datetime:")
print(sales)
print("Date column type:", sales['date'].dtype)
print()

# Extract month from dates
sales['month'] = sales['date'].dt.month
print("With extracted month:")
print(sales)

🎯 Why Work with Dates?

Temporal data analysis reveals powerful insights:

📚 What You'll Learn in This Section

Master essential date and time operations:

🔍 Date Data Types in Pandas

Understanding how pandas handles dates:

🛠️ Common Date Operations Preview

Here's what you can do with dates in pandas:

import pandas as pd

# Create sample data with various date formats
data = pd.DataFrame({
    'date_str': ['2023-01-15', '2023-02-20', '2023-03-10', '2023-04-05'],
    'sales': [100, 150, 120, 180]
})

# Convert to datetime
data['date'] = pd.to_datetime(data['date_str'])

print("Sample data:")
print(data)
print()

# Extract date components
data['year'] = data['date'].dt.year
data['month'] = data['date'].dt.month
data['day_of_week'] = data['date'].dt.day_name()

print("With extracted components:")
print(data[['date', 'year', 'month', 'day_of_week', 'sales']])
print()

# Filter by date range
recent_data = data[data['date'] >= '2023-02-01']
print("February and later:")
print(recent_data[['date', 'sales']])

📊 Time Series Analysis Preview

See how dates enable powerful time-based analysis:

import pandas as pd

# Daily sales data
daily_sales = pd.DataFrame({
    'date': pd.date_range('2023-01-01', periods=7, freq='D'),
    'sales': [100, 120, 110, 130, 140, 160, 150]
})

print("Daily sales:")
print(daily_sales)
print()

# Set date as index for time series operations
daily_sales.set_index('date', inplace=True)
print("With date index:")
print(daily_sales)
print()

# Resample to weekly totals
weekly_sales = daily_sales.resample('W').sum()
print("Weekly totals:")
print(weekly_sales)

🔧 Date Conversion Challenges

Real-world date data often needs cleaning:

📈 Business Applications

Real-world scenarios where date operations are crucial:

import pandas as pd

# Customer transactions over time
transactions = pd.DataFrame({
    'transaction_date': ['2023-01-15', '2023-01-20', '2023-02-10', '2023-02-25', '2023-03-05'],
    'customer_id': [1, 2, 1, 3, 2],
    'amount': [250, 100, 300, 150, 200]
})

# Convert to datetime
transactions['transaction_date'] = pd.to_datetime(transactions['transaction_date'])

print("Transaction data:")
print(transactions)
print()

# Extract month for analysis
transactions['month'] = transactions['transaction_date'].dt.month

# Monthly sales summary
monthly_summary = transactions.groupby('month')['amount'].agg(['sum', 'count'])
monthly_summary.columns = ['Total_Sales', 'Transaction_Count']

print("Monthly summary:")
print(monthly_summary)
print()

# Calculate days since first transaction
first_date = transactions['transaction_date'].min()
transactions['days_since_start'] = (transactions['transaction_date'] - first_date).dt.days

print("With days since start:")
print(transactions[['transaction_date', 'amount', 'days_since_start']])

🎯 Date Operation Categories

🚀 Getting Started with Dates

The key to working with dates effectively:

🚀 What's Next?

Ready to master date and time operations? Let's start with the fundamentals of converting and manipulating datetime data.

Start with: DateTime Operations

Time to work with time! 📅⏰

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent