Pandas Date and Time
Pandas provides powerful date and time processing capabilities, making it easy to convert strings to date types and perform various date-related calculations and analysis.
Creating Date and Time
date_range Function
Example
# Create date range
dates = pd.date_range("2024-01-01", periods=10, freq="D")
print("By day:")
print(dates)
print()
# By month
dates_month = pd.date_range("2024-01-01", periods=12, freq="M")
print("By month:")
print(dates_month)
print()
# By hour
dates_hour = pd.date_range("2024-01-01 00:00", periods=24, freq="H")
print("By hour (first 5):")
print(dates_hour[:5])
DatetimeIndex and DatetimeArray
Example
import numpy as np
# Create using datetime object
dt = pd.DatetimeIndex([
pd.Timestamp("2024-01-01"),
pd.Timestamp("2024-01-02"),
pd.Timestamp("2024-01-03")
])
print("DatetimeIndex:")
print(dt)
print()
# Create a Series with time
s = pd.Series(
[100, 200, 300],
index=pd.date_range("2024-01-01", periods=3, freq="D")
)
print("Series with date index:")
print(s)
Converting Strings to Dates
pd.to_datetime
Example
# Convert strings in various formats to dates
dates_str = ["2024-01-01", "2024/01/02", "01-03-2024", "20240104"]
# Automatically infer format
dt = pd.to_datetime(dates_str)
print("Automatically inferred format:")
print(dt)
print()
# Specify format
dt2 = pd.to_datetime(dates_str, format="%Y-%m-%d")
print("Specified format:")
print(dt2)
print()
# Handle invalid values
dt3 = pd.to_datetime(["2024-01-01", "invalid", "2024-01-03"], errors="coerce")
print("Handle invalid values (converted to NaT):")
print(dt3)
read_csv Automatic Date Parsing
Example
from io import StringIO
# Simulate CSV data
csv_data = """Date,Sales
2024-01-01,100
2024-01-02,200
2024-01-03,150
"""
# Method 1: Convert after reading
df = pd.read_csv(StringIO(csv_data))
df["Date"] = pd.to_datetime(df["Date"])
print("After reading, convert:")
print(df.dtypes)
print()
# Method 2: Parse directly when reading
df2 = pd.read_csv(StringIO(csv_data), parse_dates=["Date"])
print("Parse when reading:")
print(df2.dtypes)
Accessing Date Attributes
After converting date and time to datetime type, you can easily extract various attributes.
Example
# Create a date Series
s = pd.Series(pd.date_range("2024-01-15", periods=5, freq="D"))
print("Date Series:")
print(s)
print()
# Extract year/month/day
print("Extract year:")
print(s.dt.year)
print()
print("Extract month:")
print(s.dt.month)
print()
print("Extract day:")
print(s.dt.day)
print()
# Extract day of week (0=Monday, 6=Sunday)
print("Day of week (number):")
print(s.dt.dayofweek)
print()
# Extract day of week name
print("Day of week name:")
print(s.dt.day_name())
More Date Attributes
| Attribute | Description | Example |
|---|---|---|
year |
year | 2024 |
month |
Month (1-12) | 1 |
day |
Day (1-31) | 15 |
hour |
Hour (0-23) | 10 |
minute |
Minute (0-59) | 30 |
dayofweek |
Day of week (0-6) | 0 |
quarter |
Quarter (1-4) | 1 |
is_month_start |
Is beginning of month | True/False |
is_month_end |
Is end of month | True/False |
Date Operations
Date Addition and Subtraction
Example
# Create a date
date = pd.Timestamp("2024-01-15")
print(f"Base date: {date}")
print()
# Add/subtract days
print(f"+3 days: {date + pd.Timedelta(days=3)}")
print(f"-5 days: {date - pd.Timedelta(days=5)}")
print()
# Date difference
date1 = pd.Timestamp("2024-01-01")
date2 = pd.Timestamp("2024-01-15")
print(f"Date difference: {date2 - date1}")
print(f"Days difference: {(date2 - date1).days}")
print()
# Date Series operations
dates = pd.date_range("2024-01-01", periods=5, freq="D")
print("Date + 3 days:")
print(dates + pd.Timedelta(days=3))
Date Offsets
Example
date = pd.Timestamp("2024-01-15")
print(f"Base date: {date}")
print()
# Beginning/End of month
print(f"Beginning of month: {date + pd.offsets.MonthBegin(1)}")
print(f"End of month: {date + pd.offsets.MonthEnd(1)}")
print()
# Year offset
print(f"Add 1 year: {date + pd.DateOffset(years=1)}")
print(f"Subtract 1 month: {date + pd.DateOffset(months=-1)}")
print()
# Weekday
print(f"Next Monday: {date + pd.offsets.Week(weekday=0)}")
Time Zone Handling
Example
# Create time without time zone
dates = pd.date_range("2024-01-01 10:00", periods=3, freq="H")
print("Without time zone:")
print(dates)
print()
# Set time zone
dates_utc = dates.tz_localize("UTC")
print("Set UTC time zone:")
print(dates_utc)
print()
# Convert time zone
dates_shanghai = dates_utc.tz_convert("Asia/Shanghai")
print("Convert to Shanghai time zone:")
print(dates_shanghai)
Practice: Sales Data Analysis
Example
# Simulate sales data
df = pd.DataFrame({
"Date": pd.date_range("2024-01-01", periods=30, freq="D"),
"Sales": [100, 150, 120, 180, 200, 90, 80] * 4 + [100, 100]
})
df["Date"] = pd.to_datetime(df["Date"])
print("Sales data:")
print(df.head(10))
print()
# Statistics by day of week
print("Average sales by day of week:")
weekday_sales = df.groupby(df["Date"].dt.day_name())["Sales"].mean()
print(weekday_sales)
print()
# Statistics by month
print("By month:")
df["Month"] = df["Date"].dt.to_period("M")
monthly_sales = df.groupby("Month")["Sales"].sum()
print(monthly_sales)
print()
# Calculate 7-day rolling average
df["7-day rolling average"] = df["Sales"].rolling(window=7).mean()
print("Add 7-day rolling average:")
print(df.head(10))
FAQ
1. Inconsistent date formats
When usingpd.to_datetimeyou can specifyformatthe parameter to clarify the format, avoiding parsing errors.
2. Time zone confusion
When processing data across time zones, ensure all times use a unified time zone or UTC time.
3. Date operations use Timedelta
Date addition and subtraction usespd.Timedelta, rather than simple integer addition and subtraction.
Other ExtensionsWhen processing time series data, it is recommended to convert date strings to datetime type as early as possible to take advantage of Pandas' powerful date functionality.