对于不均匀时间序列,可以通过重采样的方式将其转换为均匀时间序列,再进行年度趋势分析。以下是一个示例代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 创建一个不均匀时间序列
date_rng = pd.date_range(start='1/1/2018', end='12/31/2020', freq='D')
ts = pd.Series(np.random.randint(0,100,size=(len(date_rng))), index=date_rng)
# 将不均匀时间序列转换为均匀时间序列
ts_resample = ts.resample('M').sum()
# 得到每年的均值
yearly_mean = ts_resample.groupby(ts_resample.index.year).mean()
# 绘制年度趋势图
plt.plot(yearly_mean.index, yearly_mean.values, '-o')
plt.xlabel('Year')
plt.ylabel('Value')
plt.show()
这里我们首先使用pd.date_range()
函数生成一个从2018年1月1日到2020年12月31日,以日为间隔的时间序列。再用pd.Series()
函数创建一个在此时间序列上的带有随机数的数据序列。然后使用resample()
函数将其转换为月度聚合的时间序列。接着,使用groupby()
和mean()
函数得到每年的均值。最后,用matplotlib
库绘制年度趋势图。
上一篇:不均匀时间序列的峰值检测
下一篇:布局配置