定期继续时间序列

时间:2018-07-28 08:37:24

标签: pandas time-series continuation periodicity

我有一个时间序列,其值对应一个长度为3天的周期。

import pandas as pd
import numpy as np

index = pd.date_range(start='2018-01-01', periods=3, freq='D')
values = np.arange(index.shape[0])
df = pd.DataFrame(values, index)

输出df:

            0
2018-01-01  0
2018-01-02  1
2018-01-03  2

我想继续这个系列,比如说3次,即序列[0,1,2]重复3次,并且索引以现有频率继续。是否有一种优雅的方法可以使用pd.repeat或其他等效方法来实现?我只找到了一种对它进行硬编码的方法。

n_repeats = 3
new_index = pd.date_range(start=df.index[0], periods=n_repeats*len(df), freq='D')
new_values = np.concatenate([df.values[:,0] for _ in range(n_repeats)])
new_df = pd.DataFrame(new_values, new_index)

输出new_df:

            0
2018-01-01  0
2018-01-02  1
2018-01-03  2
2018-01-04  0
2018-01-05  1
2018-01-06  2
2018-01-07  0
2018-01-08  1
2018-01-09  2

0 个答案:

没有答案