resample() requires the DataFrame, or Series, to have a DatetimeIndex, and the rule parameter uses the same frequency codes as date_range() to define the new bucket size. Like groupby(), resample() alone doesn't compute anything — it returns a lazy Resampler object that only produces a result once you chain an aggregation onto it, like .mean(), .sum(), or .ohlc(), a common finance-specific aggregation returning open/high/low/close values per bucket.
1Understanding df.resample()
resample() requires the DataFrame, or Series, to have a DatetimeIndex, and the rule parameter uses the same frequency codes as date_range() to define the new bucket size. Like groupby(), resample() alone doesn't compute anything — it returns a lazy Resampler object that only produces a result once you chain an aggregation onto it, like .mean(), .sum(), or .ohlc(), a common finance-specific aggregation returning open/high/low/close values per bucket.
resample() can either downsample, combining many fine-grained data points into fewer, coarser buckets, like daily to monthly, or upsample, creating more, finer time buckets than you have data for, which introduces gaps needing fillna()/interpolate() — know which direction you're going, since they need different follow-up handling.
import pandas as pd
dates = pd.date_range("2026-01-01", periods=6, freq="D")
df = pd.DataFrame({"sales": [10, 20, 15, 25, 30, 5]}, index=dates)
print(df.resample("3D").sum())2Practical Example
Here is a real-world application of df.resample() showing how it is used in production Pandas code.
import pandas as pd
dates = pd.date_range("2026-01-01", periods=3, freq="MS")
df = pd.DataFrame({"revenue": [1000, 1200, 900]}, index=dates)
print(df.resample("YE").sum())3Best Practices
Follow these guidelines when working with df.resample():
1. Ensure the DataFrame has a proper DatetimeIndex before calling resample()
2. Choose an aggregation appropriate to what's being summarized, sum for counts/totals, mean for rates/averages, when downsampling
3. Follow an upsampling resample() with fillna() or interpolate() to handle the new gaps it introduces, rather than leaving them as NaN unintentionally
Tip: resample() can either downsample, combining many fine-grained data points into fewer, coarser buckets, like daily to monthly, or upsample, creating more, finer time buckets than you have data for, which introduces gaps needing fillna()/interpolate() — know which direction you're going, since they need different follow-up handling.
import pandas as pd
dates = pd.date_range("2026-01-01", periods=6, freq="D")
df = pd.DataFrame({"sales": [10, 20, 15, 25, 30, 5]}, index=dates)
print(df.resample("3D").sum())