多年时间序列与“标准年份”之间的差异

时间:2015-03-11 02:36:54

标签: python datetime numpy pandas time-series

假设我有一定年限的时间序列,如:

rng = pd.date_range(start = '2001-01-01',periods = 5113)
ts = pd.TimeSeries(np.random.randn(len(rng)), rng)

我可以通过以下方式计算它的标准年份(所有年份每天的平均值):

std = ts.groupby([ts.index.month, ts.index.day]).mean()

现在我想知道如何从这个标准年减去我的多年时间序列,以获得一个时间序列,显示哪些天数低于或高于它的标准。

1 个答案:

答案 0 :(得分:1)

您可以使用groupby执行此操作,只需从该组的值中减去每个组的平均值:

average_diff = ts.groupby([ts.index.month, ts.index.day]).apply(
    lambda g: g - g.mean()
)