pandas.core.groupby.DataFrameGroupBy.resample#
- DataFrameGroupBy.resample(rule, *args, include_groups=False, **kwargs)[source]#
Provide resampling when using a TimeGrouper.
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.
- *args
Possible arguments are how, fill_method, limit, kind and on, and other arguments of TimeGrouper.
- include_groupsbool, default True
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
Possible arguments are how, fill_method, limit, kind and on, and other arguments of TimeGrouper.
- Returns:
- DatetimeIndexResampler, PeriodIndexResampler or TimdeltaResampler
Resampler object for the type of the index.
See also
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