时间序列分析与高性能Pandas操作
1. 时间序列数据重采样与频率转换
在处理时间序列数据时,经常需要对数据进行不同频率的重采样。Pandas提供了 resample()
和 asfreq()
两种方法来实现这一目的。
1.1 重采样方法对比
-
resample()
本质上是一种数据聚合操作。 -
asfreq()
本质上是一种数据选择操作。
以Google收盘价数据为例,我们对数据按业务年度末进行降采样:
goog.plot(alpha=0.5, style='-')
goog.resample('BA').mean().plot(style=':')
goog.asfreq('BA').plot(style='--')
plt.legend(['input', 'resample', 'asfreq'], loc='upper left')
在每个点上, resample()
报告的是前一年的平均值,而 asfreq()
报告的是年末的值。
1.2 升采样操作
对于升采样, resample()
和 asfreq()
在很大程度上是等效的,但 resample()
有更多的选项。