pandas.Series.resample#

Series.resample(rule, axis=_NoDefault.no_default, closed=None, label=None, convention='start', kind=_NoDefault.no_default, on=None, level=None, origin='start_day', offset=None, group_keys=False)[源代码]#

重采样时间序列数据。

用于时间序列的频率转换和重采样的便捷方法。对象必须具有类似日期时间(datetime-like)的索引(DatetimeIndexPeriodIndexTimedeltaIndex),或者调用者必须将类似日期时间的 Series/索引的标签传递给 on/level 关键字参数。

Parameters:
ruleDateOffset, Timedelta 或 str

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

axis{0 或 ‘index’, 1 或 ‘columns’}, default 0

用于向上或向下采样的轴。对于 Series,此参数未使用,默认值为 0。必须是 DatetimeIndexTimedeltaIndexPeriodIndex

自 2.0.0 版本弃用: 请改用 frame.T.resample(…)。

closed{‘right’, ‘left’},默认 None

区间哪一侧是闭合的。对于除 ‘ME’、’YE’、’QE’、’BME’、’BA’、’BQE’ 和 ‘W’ 之外的所有频率偏移,默认值为 ‘left’;对于这些偏移,默认值为 ‘right’。

label{‘right’, ‘left’},默认 None

用哪个分箱边缘标签来标记分桶。对于除 ‘ME’、’YE’、’QE’、’BME’、’BA’、’BQE’ 和 ‘W’ 之外的所有频率偏移,默认值为 ‘left’;对于这些偏移,默认值为 ‘right’。

convention{‘start’, ‘end’, ‘s’, ‘e’},默认 ‘start’

仅对于 PeriodIndex,控制是使用 rule 的开始还是结束。

kind{‘timestamp’, ‘period’},可选,默认 None

传递 ‘timestamp’ 将结果索引转换为 DateTimeIndex,或传递 ‘period’ 将其转换为 PeriodIndex。默认保留输入表示。

自 2.2.0 版本弃用: 请改为显式转换索引以获得所需类型。

onbool, default False

对于 DataFrame,用作重采样的索引的列。列必须是类似日期时间(datetime-like)的。

levelstr 或 int,可选

对于 MultiIndex,用于重采样的层(名称或编号)。level 必须是类似日期时间(datetime-like)的。

originTimestamp 或 str,默认 ‘start_day’

用于调整分组的时间戳。origin 的时区必须与索引的时区匹配。如果为字符串,则必须是以下之一:

  • ‘epoch’: origin 为 1970-01-01

  • ‘start’: origin 为时间序列的第一个值

  • ‘start_day’: origin 为时间序列的第一个日期的午夜

  • ‘end’: origin 为时间序列的最后一个值

  • ‘end_day’: origin 为最后一天的午夜上限

在 1.3.0 版本加入.

备注

仅对 Tick 频率(即固定频率,如天、小时和分钟,而不是月或季度)生效。

offsetTimedelta 或 str,默认 None

添加到 origin 的偏移量 timedelta。

group_keysbool,默认 False

在使用 .apply() 对重采样对象进行操作时,是否在结果索引中包含分组键。

在 1.5.0 版本加入: 不指定 group_keys 将保留 pandas 1.4 及更早版本的值依赖行为(有关示例,请参阅 pandas 1.5.0 Release notes )。

在 2.0.0 版本发生变更: group_keys 现在默认为 False

Returns:
pandas.api.typing.Resampler

Resampler 对象。

参见

Series.resample

重采样 Series。

DataFrame.resample

重采样 DataFrame。

groupby

通过映射、函数、标签或标签列表对 Series/DataFrame 进行分组。

asfreq

使用给定的频率重新索引 Series/DataFrame,而不进行分组。

Notes

有关更多信息,请参阅 user guide

要了解有关偏移字符串的更多信息,请参阅 this link

Examples

首先创建一个包含 7 个一分钟时间戳的系列。

>>> index = pd.date_range('1/1/2000', periods=9, freq='min')
>>> series = pd.Series(range(9), index=index)
>>> series
2000-01-01 00:00:00    0
2000-01-01 00:01:00    1
2000-01-01 00:02:00    2
2000-01-01 00:03:00    3
2000-01-01 00:04:00    4
2000-01-01 00:05:00    5
2000-01-01 00:06:00    6
2000-01-01 00:07:00    7
2000-01-01 00:08:00    8
Freq: min, dtype: int64

将系列下采样到 3 分钟的 bin,并对落入 bin 的时间戳的值求和。

>>> series.resample('3min').sum()
2000-01-01 00:00:00     3
2000-01-01 00:03:00    12
2000-01-01 00:06:00    21
Freq: 3min, dtype: int64

将系列下采样到 3 分钟的 bin,如下所示,但使用右侧边缘而不是左侧边缘标记每个 bin。请注意,用作标签的 bin 中的值不包含在它标记的 bin 中。例如,在原始系列中,bin 2000-01-01 00:03:00 包含值 3,但在带有标签 2000-01-01 00:03:00 的重采样 bin 中,包含的值不包括 3(如果包含,则总和为 6,而不是 3)。

>>> series.resample('3min', label='right').sum()
2000-01-01 00:03:00     3
2000-01-01 00:06:00    12
2000-01-01 00:09:00    21
Freq: 3min, dtype: int64

要包含此值,请关闭 bin 区间的右侧,如下所示。

>>> series.resample('3min', label='right', closed='right').sum()
2000-01-01 00:00:00     0
2000-01-01 00:03:00     6
2000-01-01 00:06:00    15
2000-01-01 00:09:00    15
Freq: 3min, dtype: int64

将 Series 升采样到 30 秒的 bin 中。

>>> series.resample('30s').asfreq()[0:5]   # Select first 5 rows
2000-01-01 00:00:00   0.0
2000-01-01 00:00:30   NaN
2000-01-01 00:01:00   1.0
2000-01-01 00:01:30   NaN
2000-01-01 00:02:00   2.0
Freq: 30s, dtype: float64

将系列上采样到 30 秒的 bin,并使用 ffill 方法填充 NaN 值。

>>> series.resample('30s').ffill()[0:5]
2000-01-01 00:00:00    0
2000-01-01 00:00:30    0
2000-01-01 00:01:00    1
2000-01-01 00:01:30    1
2000-01-01 00:02:00    2
Freq: 30s, dtype: int64

将系列上采样到 30 秒的 bin,并使用 bfill 方法填充 NaN 值。

>>> series.resample('30s').bfill()[0:5]
2000-01-01 00:00:00    0
2000-01-01 00:00:30    1
2000-01-01 00:01:00    1
2000-01-01 00:01:30    2
2000-01-01 00:02:00    2
Freq: 30s, dtype: int64

通过 ``apply` 传递自定义函数

>>> def custom_resampler(arraylike):
...     return np.sum(arraylike) + 5
...
>>> series.resample('3min').apply(custom_resampler)
2000-01-01 00:00:00     8
2000-01-01 00:03:00    17
2000-01-01 00:06:00    26
Freq: 3min, dtype: int64

对于具有 PeriodIndex 的 Series,可以使用 convention 关键字来控制是使用 rule 的开始还是结束。

使用 ‘start’ convention 将年份按季度重采样。值被分配到该期间的第一个季度。

>>> s = pd.Series(
...     [1, 2], index=pd.period_range("2012-01-01", freq="Y", periods=2)
... )
>>> s
2012    1
2013    2
Freq: Y-DEC, dtype: int64
>>> s.resample("Q", convention="start").asfreq()
2012Q1    1.0
2012Q2    NaN
2012Q3    NaN
2012Q4    NaN
2013Q1    2.0
2013Q2    NaN
2013Q3    NaN
2013Q4    NaN
Freq: Q-DEC, dtype: float64

按月重采样季度,使用 ‘end’ convention。值被分配到该期间的最后一个月。

>>> q = pd.Series(
...     [1, 2, 3, 4], index=pd.period_range("2018-01-01", freq="Q", periods=4)
... )
>>> q
2018Q1    1
2018Q2    2
2018Q3    3
2018Q4    4
Freq: Q-DEC, dtype: int64
>>> q.resample("M", convention="end").asfreq()
2018-03    1.0
2018-04    NaN
2018-05    NaN
2018-06    2.0
2018-07    NaN
2018-08    NaN
2018-09    3.0
2018-10    NaN
2018-11    NaN
2018-12    4.0
Freq: M, dtype: float64

对于 DataFrame 对象,可以使用 on 关键字指定列而不是索引进行重采样。

>>> d = {'price': [10, 11, 9, 13, 14, 18, 17, 19],
...      'volume': [50, 60, 40, 100, 50, 100, 40, 50]}
>>> df = pd.DataFrame(d)
>>> df['week_starting'] = pd.date_range('01/01/2018',
...                                     periods=8,
...                                     freq='W')
>>> df
   price  volume week_starting
0     10      50    2018-01-07
1     11      60    2018-01-14
2      9      40    2018-01-21
3     13     100    2018-01-28
4     14      50    2018-02-04
5     18     100    2018-02-11
6     17      40    2018-02-18
7     19      50    2018-02-25
>>> df.resample('ME', on='week_starting').mean()
               price  volume
week_starting
2018-01-31     10.75    62.5
2018-02-28     17.00    60.0

对于具有 MultiIndex 的 DataFrame,可以使用 level 关键字指定需要进行重采样的级别。

>>> days = pd.date_range('1/1/2000', periods=4, freq='D')
>>> d2 = {'price': [10, 11, 9, 13, 14, 18, 17, 19],
...       'volume': [50, 60, 40, 100, 50, 100, 40, 50]}
>>> df2 = pd.DataFrame(
...     d2,
...     index=pd.MultiIndex.from_product(
...         [days, ['morning', 'afternoon']]
...     )
... )
>>> df2
                      price  volume
2000-01-01 morning       10      50
           afternoon     11      60
2000-01-02 morning        9      40
           afternoon     13     100
2000-01-03 morning       14      50
           afternoon     18     100
2000-01-04 morning       17      40
           afternoon     19      50
>>> df2.resample('D', level=0).sum()
            price  volume
2000-01-01     21     110
2000-01-02     22     140
2000-01-03     32     150
2000-01-04     36      90

如果要根据固定时间戳调整 bin 的开始:

>>> start, end = '2000-10-01 23:30:00', '2000-10-02 00:30:00'
>>> rng = pd.date_range(start, end, freq='7min')
>>> ts = pd.Series(np.arange(len(rng)) * 3, index=rng)
>>> ts
2000-10-01 23:30:00     0
2000-10-01 23:37:00     3
2000-10-01 23:44:00     6
2000-10-01 23:51:00     9
2000-10-01 23:58:00    12
2000-10-02 00:05:00    15
2000-10-02 00:12:00    18
2000-10-02 00:19:00    21
2000-10-02 00:26:00    24
Freq: 7min, dtype: int64
>>> ts.resample('17min').sum()
2000-10-01 23:14:00     0
2000-10-01 23:31:00     9
2000-10-01 23:48:00    21
2000-10-02 00:05:00    54
2000-10-02 00:22:00    24
Freq: 17min, dtype: int64
>>> ts.resample('17min', origin='epoch').sum()
2000-10-01 23:18:00     0
2000-10-01 23:35:00    18
2000-10-01 23:52:00    27
2000-10-02 00:09:00    39
2000-10-02 00:26:00    24
Freq: 17min, dtype: int64
>>> ts.resample('17min', origin='2000-01-01').sum()
2000-10-01 23:24:00     3
2000-10-01 23:41:00    15
2000-10-01 23:58:00    45
2000-10-02 00:15:00    45
Freq: 17min, dtype: int64

如果要使用 offset Timedelta 调整 bin 的开始,以下两行是等效的:

>>> ts.resample('17min', origin='start').sum()
2000-10-01 23:30:00     9
2000-10-01 23:47:00    21
2000-10-02 00:04:00    54
2000-10-02 00:21:00    24
Freq: 17min, dtype: int64
>>> ts.resample('17min', offset='23h30min').sum()
2000-10-01 23:30:00     9
2000-10-01 23:47:00    21
2000-10-02 00:04:00    54
2000-10-02 00:21:00    24
Freq: 17min, dtype: int64

如果要将最大时间戳作为 bin 的结束:

>>> ts.resample('17min', origin='end').sum()
2000-10-01 23:35:00     0
2000-10-01 23:52:00    18
2000-10-02 00:09:00    27
2000-10-02 00:26:00    63
Freq: 17min, dtype: int64

start_day 相反,您可以使用 end_day 将最大时间戳的午夜作为 bin 的结束,并删除不包含数据的 bin:

>>> ts.resample('17min', origin='end_day').sum()
2000-10-01 23:38:00     3
2000-10-01 23:55:00    15
2000-10-02 00:12:00    45
2000-10-02 00:29:00    45
Freq: 17min, dtype: int64