pandas.core.groupby.DataFrameGroupBy.resample#
- DataFrameGroupBy.resample(rule, *args, include_groups=True, **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.
Deprecated since version 2.2.0: Setting include_groups to True is deprecated. Only the value False will be allowed in a future version of pandas.
- **kwargs
Possible arguments are how, fill_method, limit, kind and on, and other arguments of TimeGrouper.
- Returns:
- pandas.api.typing.DatetimeIndexResamplerGroupby,
- pandas.api.typing.PeriodIndexResamplerGroupby, or
- pandas.api.typing.TimedeltaIndexResamplerGroupby
Return a new groupby object, with type depending on the data being resampled.
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', include_groups=False).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', include_groups=False).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', include_groups=False).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', include_groups=False) ... .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', include_groups=False) ... .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