pythonbeginner
Pandas Time-Series Resampling
Resample time-series data from daily to weekly/monthly frequencies with aggregation functions.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import numpy as np
idx = pd.date_range('2024-01-01', periods=365, freq='D')
df = pd.DataFrame({'revenue': np.random.randint(100, 1000, 365)}, index=idx)
weekly = df.resample('W').sum()
monthly = df.resample('ME').agg({'revenue': ['sum', 'mean', 'max']})
print('Weekly:\n', weekly.head())
print('Monthly:\n', monthly)Use Cases
- time-series analytics
- reporting dashboards
- KPI aggregation
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 Time Series Analysis
Time series operations with resampling, rolling windows, date offsets, and period conversions.
Best for: Sales trend analysis with moving averages
#pandas#time-series
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
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