pandas.Series.resample#
- Series.resample(rule, axis=0, closed=None, label=None, convention='start', kind=None, loffset=None, base=None, on=None, level=None, origin='start_day', offset=None, group_keys=NoDefault.no_default)[源代码]#
重新采样时间序列数据。
一种方便的时间序列频率变换和重采样方法。该对象必须具有类似DateTime的索引 (DatetimeIndex , PeriodIndex ,或 TimedeltaIndex ),否则调用方必须将类似DateTime的序列/索引的标签传递给
on
/level
关键字参数。- 参数
- ruleDateOffset、Timedelta或字符串
表示目标转换的偏移量字符串或对象。
- axis{0或‘index’,1或‘Columns’},默认为0
使用哪个轴进行向上采样或向下采样。为 Series 这将默认为0,即沿行。一定是 DatetimeIndex , TimedeltaIndex 或 PeriodIndex 。
- closed{‘右’,‘左’},默认为无
仓位间隔的哪一侧闭合。所有频率偏移的缺省值都是‘Left’,但‘M’、‘A’、‘Q’、‘BM’、‘BA’、‘BQ’和‘W’的缺省值都是‘Right’。
- label{‘右’,‘左’},默认为无
用哪个料箱边缘标签来标记料桶。所有频率偏移的缺省值都是‘Left’,但‘M’、‘A’、‘Q’、‘BM’、‘BA’、‘BQ’和‘W’的缺省值都是‘Right’。
- convention{‘Start’,‘End’,‘s’,‘e’},默认‘Start’
为 PeriodIndex 仅用于控制是使用 rule 。
- kind{‘Timestamp’,‘Period’},可选,默认为无
传递“”Timestamp“”以将结果索引转换为 DateTimeIndex 或“Period”将其转换为 PeriodIndex 。默认情况下,将保留输入表示法。
- loffset时间增量,默认为无
调整重新采样的时间标签。
1.1.0 版后已移除: 您应该将loffset添加到 df.index 在重新采样之后。请参见下面的内容。
- base整型,默认为0
对于平均细分为1天的频率,为合计间隔的“原点”。例如,对于“5分钟”频率,基准的范围可以是0到4。缺省值为0。
1.1.0 版后已移除: 您应该使用的新参数是‘Offset’或‘Origin’。
- on字符串,可选
对于DataFrame,要使用的列而不是用于重采样的索引。列必须类似于DateTime。
- level字符串或int,可选
对于多索引,用于重采样的级别(名称或编号)。 level 必须是类似约会时间的。
- originTIMESTAMP或STR,默认为‘start_day’
调整分组所依据的时间戳。来源时区必须与索引的时区匹配。如果为字符串,则必须为以下值之一:
《纪元》: origin 是1970-01-01
‘开始’: origin 是时间序列的第一个价值
‘START_DAY’: origin 是时间系列片的第一天午夜
1.1.0 新版功能.
‘End’: origin 是时间序列的最后一个值
‘End_day’: origin 最后一天的天花板是午夜吗?
1.3.0 新版功能.
- offsetTimedelta或str,默认为无
添加到原点的偏移时间增量。
1.1.0 新版功能.
- group_keys布尔值,可选
使用时是否将组密钥包括在结果索引中
.apply()
在重新采样的对象上。未指定group_keys
将保留Pandas1.4及更早版本中依赖于值的行为(请参见 pandas 1.5.0 Release notes 例如)。在未来的Pandas版本中,该行为将默认为与指定group_keys=False
。1.5.0 新版功能.
- 退货
- pandas.core.Resampler
Resampler
对象。
参见
Series.resample
对系列进行重新采样。
DataFrame.resample
对DataFrame重新采样。
groupby
按映射、函数、标签或标签列表对系列进行分组。
asfreq
在不分组的情况下,对具有给定频率的系列重新编制索引。
注意事项
请参阅 user guide 想要更多。
欲了解更多偏移量字符串,请参阅 this link 。
示例
首先创建一个带有9个一分钟时间戳的系列。
>>> index = pd.date_range('1/1/2000', periods=9, freq='T') >>> 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: T, dtype: int64
将序列下采样到3分钟箱中,并将落入箱中的时间戳的值相加。
>>> series.resample('3T').sum() 2000-01-01 00:00:00 3 2000-01-01 00:03:00 12 2000-01-01 00:06:00 21 Freq: 3T, dtype: int64
如上所述,将序列下采样到3分钟的箱中,但使用右侧而不是左侧对每个箱进行标记。请注意,用作标签的存储桶中的值不包括在它所标记的存储桶中。例如,在最初的系列中,桶
2000-01-01 00:03:00
包含值3,但重新采样的存储桶中的总和值与标签2000-01-01 00:03:00
不包括3(如果包含,则求和值将为6,而不是3)。要包括此值,请关闭bin间隔的右侧,如下面的示例所示。>>> series.resample('3T', 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: 3T, dtype: int64
如上所述,将序列向下采样为3分钟箱,但关闭箱间隔的右侧。
>>> series.resample('3T', 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: 3T, dtype: int64
将该系列向上采样到30秒的箱中。
>>> 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秒的箱中,并填写
NaN
值使用pad
方法。>>> series.resample('30S').pad()[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秒的箱中,并填写
NaN
值使用bfill
方法。>>> 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('3T').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: 3T, dtype: int64
对于具有周期索引的系列,关键字 convention 可用于控制是使用 rule 。
使用‘Start’逐个季度重新采样 convention 。将值分配给期间的第一个季度。
>>> s = pd.Series([1, 2], index=pd.period_range('2012-01-01', ... freq='A', ... periods=2)) >>> s 2012 1 2013 2 Freq: A-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('M', on='week_starting').mean() price volume week_starting 2018-01-31 10.75 62.5 2018-02-28 17.00 60.0
对于具有多索引的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
如果您想要根据固定的时间戳调整垃圾箱的起始位置:
>>> 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: 7T, 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: 17T, 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: 17T, 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: 17T, dtype: int64
如果要使用 offset Timedelta,以下两行是等价的:
>>> 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: 17T, 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: 17T, dtype: int64
如果您想将最大的时间戳作为垃圾桶的末尾:
>>> 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: 17T, dtype: int64
与 start_day ,您可以使用 end_day 要将最大时间戳的天花板午夜作为垃圾桶的末尾,并丢弃不包含数据的垃圾桶:
>>> 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: 17T, dtype: int64
取代不推荐使用的 base 参数,您现在可以使用 offset 在本例中,它等效于具有 base=2 :
>>> ts.resample('17min', offset='2min').sum() 2000-10-01 23:16:00 0 2000-10-01 23:33:00 9 2000-10-01 23:50:00 36 2000-10-02 00:07:00 39 2000-10-02 00:24:00 24 Freq: 17T, dtype: int64
取代不推荐使用的 loffset 论点:
>>> from pandas.tseries.frequencies import to_offset >>> loffset = '19min' >>> ts_out = ts.resample('17min').sum() >>> ts_out.index = ts_out.index + to_offset(loffset) >>> ts_out 2000-10-01 23:33:00 0 2000-10-01 23:50:00 9 2000-10-02 00:07:00 21 2000-10-02 00:24:00 54 2000-10-02 00:41:00 24 Freq: 17T, dtype: int64