API · Pandas

Pandas DataFrame.resample() Function

The DataFrame resample() function resamples the time-series data.

On this page

Python Pandas DataFrame.resample() groups time-indexed observations into frequency-based intervals so you can aggregate them, such as by summing each week or month.

Syntax of pandas.DataFrame.resample()

The DataFrame method resamples rows using a datetime-like index, or a datetime-like column or index level selected with on or level. The exact signature varies across pandas versions; the core call is df.resample(rule, ...), where rule specifies the target frequency. No pandas runtime version is specified here, so version-specific defaults and parameters are intentionally omitted.

Parameters

Parameter Purpose
rule Frequency rule that defines the target intervals.
closed Chooses which side of each interval is included.
label Chooses which interval edge labels each result.
on Selects a datetime-like DataFrame column for grouping instead of the index.
level Selects a datetime-like level of a MultiIndex for grouping.
origin and offset Adjust where the intervals begin; their accepted values and behavior depend on the pandas version.

Return

resample() returns a resampler object. Chain an aggregation such as .sum() to produce a result for each interval. DataFrame.resample() operates on a DataFrame; the examples below use a Series and therefore call Series.resample() instead. The examples are unexecuted in this packet, which supplies no pandas runtime version or execution evidence, so no observed output is asserted.

Weekly resampling example

This Series contains one value for each date from January 1 through January 30, 2021. The weekly rule groups those daily values into weekly bins, and .sum() adds the values in each bin. With the Sunday-ending weekly rule shown, the labels are the bin-ending Sundays; the first and final bins are partial because the supplied dates begin on Friday and stop on Saturday.

import pandas as pd

dates = pd.date_range("2021-01-01", periods=30, freq="D")
daily = pd.Series(range(30), index=dates)
weekly_totals = daily.resample("W").sum()
print(weekly_totals)

This snippet is not executed evidence; it shows the Series operation and input needed to adapt the aggregation.

Monthly resampling example

The same daily Series can be grouped into a calendar-month interval and summed. This code reports the sum of the supplied January 1–30 observations: the values run from 0 through 29, so their sum is 435. It is not a complete-month aggregate because the input has no January 31 observation. MonthEnd() expresses the month-ending rule without relying on a frequency alias whose support may vary by pandas version.

import pandas as pd

dates = pd.date_range("2021-01-01", periods=30, freq="D")
daily = pd.Series(range(30), index=dates)
monthly_totals = daily.resample(pd.offsets.MonthEnd()).sum()
print(monthly_totals)

This snippet is not executed evidence; its result is described from the supplied values, and pandas-version-specific formatting is not asserted.