时间序列的索引与切片
索引
时间序列的索引方法同样是适用于Dataframe,而且在时间序列中由于按照时间先后排序,故不用考虑顺序问题。
基本位置索引,使用的方法和列表类似:
from datetime import datetime
rng = pd.date_range('2017/1','2017/3')
ts = pd.Series(np.random.rand(len(rng)), index = rng)
print(ts.head())
print(ts[0])
print(ts[:2])
>>>
2017-01-01 0.107736
2017-01-02 0.887981
2017-01-03 0.712862
2017-01-04 0.920021
2017-01-05 0.317863
Freq: D, dtype: float64
0.107735945027
2017-01-01 0.107736
2017-01-02 0.887981
Freq: D, dtype: float64
除了基本位置索引之外还有时间序列标签索引:
from datetime import datetime
rng = pd.date_range('2017/1','2017/3')
ts = pd.Series(np.random.rand(len(rng)), index &