pandas.api.typing.DataFrameGroupBy.resample#

DataFrameGroupBy.resample(rule, closed=None, label=None, convention=None, on=None, origin='start_day', offset=None, group_keys=False, *, include_groups=False, **kwargs)[source]#

Provide resampling within each group of a groupby.

Given a grouper, the function resamples it according to a string “string” -> “frequency”.

See the frequency aliases documentation for more details.

Parameters:
rulestr or DateOffset

The offset string or object representing target grouper conversion.

closed{‘right’, ‘left’}, optional

Which side of bin interval is closed. See Series.resample().

label{‘right’, ‘left’}, optional

Which bin edge label to label bucket with. See Series.resample().

convention{‘start’, ‘end’, ‘s’, ‘e’}, optional

For PeriodIndex only. See Series.resample().

onstr, optional

For a DataFrame, column to use instead of index for resampling. Column must be datetime-like.

originTimestamp or str, default ‘start_day’

The timestamp on which to adjust the grouping. See Series.resample() for accepted string values.

offsetTimedelta or str, optional

An offset timedelta added to the origin.

group_keysbool, default False

Whether to include the group keys in the result index when using .apply() on the resampled object.

include_groupsbool, default False

When True, will attempt to include the groupings in the operation in the case that they are columns of the DataFrame. If this raises a TypeError, the result will be computed with the groupings excluded. When False, the groupings will be excluded when applying func.

Added in version 2.2.0.

Changed in version 3.0: The default was changed to False, and True is no longer allowed.

**kwargs

Additional keyword arguments forwarded to the underlying Grouper.

Returns:
DatetimeIndexResampler, PeriodIndexResampler or TimedeltaResampler

Resampler object for the type of the index.

See also

Series.resample

Resample a Series.

DataFrame.resample

Resample a DataFrame.

Grouper

Specify a frequency to resample with when grouping by a key.

DatetimeIndex.resample

Frequency conversion and resampling of time series.

Examples

>>> idx = pd.date_range("1/1/2000", periods=4, freq="min")
>>> df = pd.DataFrame(data=4 * [range(2)], index=idx, columns=["a", "b"])
>>> df.iloc[2, 0] = 5
>>> df
                    a  b
2000-01-01 00:00:00  0  1
2000-01-01 00:01:00  0  1
2000-01-01 00:02:00  5  1
2000-01-01 00:03:00  0  1

Downsample the DataFrame into 3 minute bins and sum the values of the timestamps falling into a bin.

>>> df.groupby("a").resample("3min").sum()
                         b
a
0   2000-01-01 00:00:00  2
    2000-01-01 00:03:00  1
5   2000-01-01 00:00:00  1

Upsample the series into 30 second bins.

>>> df.groupby("a").resample("30s").sum()
                    b
a
0   2000-01-01 00:00:00  1
    2000-01-01 00:00:30  0
    2000-01-01 00:01:00  1
    2000-01-01 00:01:30  0
    2000-01-01 00:02:00  0
    2000-01-01 00:02:30  0
    2000-01-01 00:03:00  1
5   2000-01-01 00:02:00  1

Resample by month. Values are assigned to the month of the period.

>>> df.groupby("a").resample("ME").sum()
            b
a
0   2000-01-31  3
5   2000-01-31  1

Downsample the series into 3 minute bins as above, but close the right side of the bin interval.

>>> (df.groupby("a").resample("3min", closed="right").sum())
                         b
a
0   1999-12-31 23:57:00  1
    2000-01-01 00:00:00  2
5   2000-01-01 00:00:00  1

Downsample the series into 3 minute bins and close the right side of the bin interval, but label each bin using the right edge instead of the left.

>>> (df.groupby("a").resample("3min", closed="right", label="right").sum())
                         b
a
0   2000-01-01 00:00:00  1
    2000-01-01 00:03:00  2
5   2000-01-01 00:03:00  1