pythonintermediate
Pandas Time Series Analysis
Time series operations with resampling, rolling windows, date offsets, and period conversions.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import numpy as np
# Create time series data
dates = pd.date_range("2024-01-01", periods=365, freq="D")
df = pd.DataFrame({
"date": dates,
"sales": np.random.randint(100, 1000, 365).astype(float),
"visitors": np.random.randint(500, 5000, 365),
})
df = df.set_index("date")
# Resample to weekly/monthly aggregates
weekly = df.resample("W").agg({"sales": "sum", "visitors": "mean"})
monthly = df.resample("ME").agg({"sales": "sum", "visitors": "sum"})
# Rolling window calculations
df["sales_7d_avg"] = df["sales"].rolling(window=7).mean()
df["sales_30d_avg"] = df["sales"].rolling(window=30).mean()
df["sales_7d_std"] = df["sales"].rolling(window=7).std()
# Expanding window (cumulative)
df["cumulative_sales"] = df["sales"].expanding().sum()
# Percent change
df["daily_change"] = df["sales"].pct_change()
df["weekly_change"] = df["sales"].pct_change(periods=7)
# Shift for lag features
df["sales_yesterday"] = df["sales"].shift(1)
df["sales_last_week"] = df["sales"].shift(7)
# Filter by date range
q1 = df.loc["2024-01":"2024-03"]
jan = df.loc["2024-01"]
print(f"Monthly summary:\n{monthly.head()}")
print(f"\nQ1 total sales: {q1['sales'].sum():,.0f}")Use Cases
- Sales trend analysis with moving averages
- Building lag features for forecasting models
- Time-based aggregation for dashboards
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Pandas GroupBy Aggregation Examples
GroupBy operations with multiple aggregations, named aggregations, and transform for DataFrame analysis.
Best for: Sales reporting by region and time period
#pandas#groupby
pythonintermediate
Pandas Rolling & Expanding Windows
Compute moving averages, rolling sums, and cumulative stats on time-series data with pandas.
Best for: sales forecasting
#pandas#time-series
pythonbeginner
Pandas Time-Series Resampling
Resample time-series data from daily to weekly/monthly frequencies with aggregation functions.
Best for: time-series analytics
#pandas#time-series
pythonintermediate
Pandas merge_asof for Time-Based Joins
Perform an as-of join to match events to the most recent reference record within a time window.
Best for: tick data joins
#pandas#merge-asof