pandas.core.groupby.DataFrameGroupBy.resample#
- DataFrameGroupBy.resample(rule, *args, include_groups=True, **kwargs)[源代码]#
使用 TimeGrouper 时提供重采样。
给定一个分组器,该函数根据字符串“string”->“frequency”对其进行重采样。
See the frequency aliases documentation for more details.
- Parameters:
- rulestr 或 DateOffset
表示目标分组器转换的偏移字符串或对象。
- *args
可能的参数有 how、fill_method、limit、kind 和 on,以及 TimeGrouper 的其他参数。
- include_groupsbool, default True
如果为 True,将尝试在它们是 DataFrame 列的情况下将分组包含在操作中。如果这会引发 TypeError,则将排除分组计算结果。如果为 False,则在应用
func时将排除分组。在 2.2.0 版本加入.
自 2.2.0 版本弃用: 将 include_groups 设置为 True 已弃用。在 pandas 的未来版本中只允许 False 值。
- **kwargs
可能的参数有 how、fill_method、limit、kind 和 on,以及 TimeGrouper 的其他参数。
- Returns:
- pandas.api.typing.DatetimeIndexResamplerGroupby,
- pandas.api.typing.PeriodIndexResamplerGroupby, or
- pandas.api.typing.TimedeltaIndexResamplerGroupby
返回一个新的 groupby 对象,其类型取决于正在重采样的数据。
参见
Grouper按关键字分组时,指定重采样频率。
DatetimeIndex.resample时间序列的频率转换和重采样。
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
将 DataFrame 降采样到 3 分钟的 bin 中,并对落入 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
将 Series 升采样到 30 秒的 bin 中。
>>> 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
按月重采样。值被分配到期间的月份。
>>> df.groupby('a').resample('ME', include_groups=False).sum() b a 0 2000-01-31 3 5 2000-01-31 1
将 Series 降采样到 3 分钟的 bin 中,如上所述,但关闭 bin 区间的右侧。
>>> ( ... 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
将 Series 降采样到 3 分钟的 bin 中,并关闭 bin 区间的右侧,但使用右边缘而不是左边缘标记每个 bin。
>>> ( ... 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