🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.resample()

AI & DATA SCIENCE // df-resample

df.resample() groups time series data into new, regular time buckets (like daily data into monthly buckets), then applies an aggregation to each bucket — the time-series-specific counterpart to groupby().

Syntax

df.resample(rule).agg_method()

Deep Dive Course

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.

editor.html
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())
localhost:3000

2Practical Example

Here is a real-world application of df.resample() showing how it is used in production Pandas code.

editor.html
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())
localhost:3000

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.

editor.html
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())
localhost:3000

Examples

Example 01Basic Usage
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())
Example 02Advanced Example
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())

Best Practices

  • Ensure the DataFrame has a proper DatetimeIndex before calling resample()
  • Choose an aggregation appropriate to what's being summarized, sum for counts/totals, mean for rates/averages, when downsampling
  • Follow an upsampling resample() with fillna() or interpolate() to handle the new gaps it introduces, rather than leaving them as NaN unintentionally

Interview Question

Why does df.resample() require the DataFrame to have a DatetimeIndex, unlike groupby(), which can group by any column?

Hint: Think about what resample() actually needs to know to build its time-based buckets.

resample() builds its groups based on time intervals — it needs to know exactly where each row falls chronologically to determine which time bucket it belongs to, and a DatetimeIndex is what gives pandas that chronological information directly and efficiently, in a form it can reason about with calendar/frequency logic. A plain groupby() only needs to compare values for exact equality to form groups, which works for any column of any type, but resample()'s time-bucketing logic specifically depends on understanding dates and durations, which only a proper datetime-typed index provides.

Exercises

MediumPractice using df.resample() in a real scenario.
View Solution
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())

Frequently Asked Questions

Why does df.resample() require the DataFrame to have a DatetimeIndex, unlike groupby(), which can group by any column?

resample() builds its groups based on time intervals — it needs to know exactly where each row falls chronologically to determine which time bucket it belongs to, and a DatetimeIndex is what gives pandas that chronological information directly and efficiently, in a form it can reason about with calendar/frequency logic. A plain groupby() only needs to compare values for exact equality to form groups, which works for any column of any type, but resample()'s time-bucketing logic specifically depends on understanding dates and durations, which only a proper datetime-typed index provides.

Related Functions

pd-date-rangedf-groupbydf-shift