37. Pandas的时间序列数据-period_range

可以通过pandas的period_range函数产生时间序列作为series的index。

import pandas as pd
import numpy as np
att = ["S", "T", "H", "D", "M", "A"]
vi = np.random.randn(5)
for a in att:
    pi = pd.period_range('2018-12-19 11:22:33', periods = 5, freq= a)
    ts = pd.Series(vi, index = pi)
    print ts, "\n"

程序的执行结果:

2018-12-19 11:22:33   -0.275161
2018-12-19 11:22:34   -0.763390
2018-12-19 11:22:35   -2.012351
2018-12-19 11:22:36   -1.126492
2018-12-19 11:22:37    0.843842
Freq: S, dtype: float64 

2018-12-19 11:22   -0.275161
2018-12-19 11:23   -0.763390
2018-12-19 11:24   -2.012351
2018-12-19 11:25   -1.126492
2018-12-19 11:26    0.843842
Freq: T, dtype: float64 

2018-12-19 11:00   -0.275161
2018-12-19 12:00   -0.763390
2018-12-19 13:00   -2.012351
2018-12-19 14:00   -1.126492
2018-12-19 15:00    0.843842
Freq: H, dtype: float64 

2018-12-19   -0.275161
2018-12-20   -0.763390
2018-12-21   -2.012351
2018-12-22   -1.126492
2018-12-23    0.843842
Freq: D, dtype: float64 

2018-12   -0.275161
2019-01   -0.763390
2019-02   -2.012351
2019-03   -1.126492
2019-04    0.843842
Freq: M, dtype: float64 

2018   -0.275161
2019   -0.763390
2020   -2.012351
2021   -1.126492
2022    0.843842
Freq: A-DEC, dtype: float64