简介
本文接着介绍时间频率转换和重新对齐的两个方法( resample 和 reindex )。
resample()方法
resample 主要用于将数据转换到固定频率。先来看一个例子:
1 2 3 4 5 6 7 8 9 |
>>>import numpy as np >>>import pandas as pd >>>a = pd.Series(np.random.rand(3), index=pd.date_range('12/8/2018', periods=3, freq='W-SAT')) >>>a 2018-12-08 0.785838 2018-12-15 0.651768 2018-12-22 0.489593 Freq: W-SAT, dtype: float64 |
在这里我们创建了一个Series,使用 date_range 产生周期索引,频率 'W-SAT' 表示按每周的星期六开始为一个周期,所以第一个星期六为 2018-12-08 ,第二个为 2018-12-15 ,以此类推。
接着重采样到工作日频率:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
>>>a.resample('D').asfreq() 2018-12-08 0.785838 2018-12-09 NaN 2018-12-10 NaN 2018-12-11 NaN 2018-12-12 NaN 2018-12-13 NaN 2018-12-14 NaN 2018-12-15 0.651768 2018-12-16 NaN 2018-12-17 NaN 2018-12-18 NaN 2018-12-19 NaN 2018-12-20 NaN 2018-12-21 NaN 2018-12-22 0.489593 Freq: D, dtype: float64 |
可以传入 fill_method 参数控制填充 NaN的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 前向填充 >>>a.resample('D', fill_method='ffill') 2018-12-08 0.785838 2018-12-09 0.785838 2018-12-10 0.785838 2018-12-11 0.785838 2018-12-12 0.785838 2018-12-13 0.785838 2018-12-14 0.785838 2018-12-15 0.651768 2018-12-16 0.651768 2018-12-17 0.651768 2018-12-18 0.651768 2018-12-19 0.651768 2018-12-20 0.651768 2018-12-21 0.651768 2018-12-22 0.489593 Freq: D, dtype: float64 # 后向填充 >>>a.resample('D', fill_method='bfill') 2018-12-08 0.785838 2018-12-09 0.651768 2018-12-10 0.651768 2018-12-11 0.651768 2018-12-12 0.651768 2018-12-13 0.651768 2018-12-14 0.651768 2018-12-15 0.651768 2018-12-16 0.489593 2018-12-17 0.489593 2018-12-18 0.489593 2018-12-19 0.489593 2018-12-20 0.489593 2018-12-21 0.489593 2018-12-22 0.489593 Freq: D, dtype: float64 |
通过 resample 方法改变周期频率后,还能使用一些简单的计算方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
>>>b = pd.Series(range(9), index=pd.date_range('12/8/2018', periods=9, freq='T')) >>>b 2018-12-08 00:00:00 0 2018-12-08 00:01:00 1 2018-12-08 00:02:00 2 2018-12-08 00:03:00 3 2018-12-08 00:04:00 4 2018-12-08 00:05:00 5 2018-12-08 00:06:00 6 2018-12-08 00:07:00 7 2018-12-08 00:08:00 8 Freq: T, dtype: int64 >>>b.resample('3T').sum() 2018-12-08 00:00:00 3 2018-12-08 00:03:00 12 2018-12-08 00:06:00 21 Freq: 3T, dtype: int64 |
可以看出重采样以 '3T' 作为频率后,再通过 sum 计算每三分钟的总和。
reindex()方法
我们知道当直接对两个Series进行加和时,会根据对应的索引值对齐加和:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
>>>c = pd.Series(np.arange(4), index=pd.date_range('12/8/2018', periods=4)) >>>c 2018-12-08 0 2018-12-09 1 2018-12-10 2 2018-12-11 3 Freq: D, dtype: int32 >>>d = pd.Series(np.arange(4), index=pd.date_range('12/10/2018', periods=4)) >>>d 2018-12-10 0 2018-12-11 1 2018-12-12 2 2018-12-13 3 Freq: D, dtype: int32 >>>c + d 2018-12-08 NaN 2018-12-09 NaN 2018-12-10 2.0 2018-12-11 4.0 2018-12-12 NaN 2018-12-13 NaN |
就像上面的那样,只有索引值相同,并且非 NaN 值才会有结果。
如果此时想将 c 中的值加到 d 中去并保持 d 的日期索引,就可以使用 reindex 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>>c.reindex(d.index, method='ffill') 2018-12-10 2 2018-12-11 3 2018-12-12 3 2018-12-13 3 Freq: D, dtype: int32 >>>d + c.reindex(d.index, method='ffill') 2018-12-10 2 2018-12-11 4 2018-12-12 5 2018-12-13 6 Freq: D, dtype: int32 |
重新对齐索引后,会产生 NaN 值,通过 method 选择填充方法,变成一样的索引值后就能相加了。
总结
本文介绍了处理时间序列的两种方法:一种是重采样 resample 方法,能改变周期频率;另一种是重索引 reindex 方法,可以用来对齐时间索引,方便运算。