pandas.core.groupby.SeriesGroupBy.resample #

系列分组依据。重新采样(规则, * args , include_groups = True , ** kwargs ) [来源] #

使用 TimeGrouper 时提供重采样。

给定一条石斑鱼,该函数根据字符串“string”->“Frequency”对其进行重新采样。

有关更多详细信息,请参阅频率别名文档。

参数
规则str 或 DateOffset

表示目标石斑鱼转换的偏移字符串或对象。

*参数

可能的参数有howfill_methodlimitkindon ,以及TimeGrouper的其他参数。

include_groups bool, 默认 True

当为 True 时,将尝试在操作中包含分组(如果它们是 DataFrame 的列)。如果这引发类型错误,则将在排除分组的情况下计算结果。当为 False 时,应用时将排除分组func

2.2.0 版本中的新增内容。

自版本 2.2.0 起已弃用:已弃用将 include_groups 设置为 True。 pandas 的未来版本中只允许使用 False 值。

**夸格

可能的参数有howfill_methodlimitkindon ,以及TimeGrouper的其他参数。

返回
pandas.api.typing.DatetimeIndexResamplerGroupby,
pandas.api.typing.PeriodIndexResamplerGroupby,或
pandas.api.typing.TimedeltaIndexResamplerGroupby

返回一个新的 groupby 对象,其类型取决于重新采样的数据。

也可以看看

Grouper

指定按键分组时重新采样的频率。

DatetimeIndex.resample

时间序列的频率转换和重采样。

例子

>>> 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

将系列上采样为 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

如上所述,将系列下采样为 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

将系列下采样为 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