TimeSeries是Series的一个子类,所以Series索引及数据选取方面的方法基本一样
同时TimeSeries通过时间序列有更便捷的方法做索引和切片
# 索引
from datetime import datetime
rng = pd.date_range('2017/1','2017/3')
ts = pd.Series(np.random.rand(len(rng)), index = rng)
print("1".center(40,'*'))
print(ts.head(),len(rng))
print("2".center(40,'*'))
print(ts[0])
print(ts[:2])
# 基本下标位置索引
print("3".center(40,'*'))
print(ts['2017/1/2'])
print(ts['20170103'])
print(ts['1/10/2017'])
print(ts[datetime(2017,1,20)])
# 时间序列标签索引,支持各种时间字符串,以及datetime.datetime
# 时间序列由于按照时间先后排序,故不用考虑顺序问题
# 索引方法同样适用于Dataframe
#执行结果
*******************1********************
2017-01-01 0.805366
2017-01-02 0.522639
2017-01-03 0.834465
2017-01-04 0.523208
2017-01-05 0.366457
Freq: D, dtype: float64 60
*******************2********************
0.8053663430822671
2017-01-01 0.805366
2017-01-02 0.522639
Freq: D, dtype: float64
*******************3********************
0.5226389965722876
0.8344647351315494
0.3108613679822997
0.8222941491109566
# 切片
rng = pd.date_range('2017/1','2017/3',freq = '12H')
ts = pd.Series(np.random.rand(len(rng)), index = rng)
print(ts['2017/1/5':'2017/1/10'])
print("1".center(40,'*'))
# 和Series按照index索引原理一样,也是末端包含
print(ts['2017/2'].head())
# 传入月,直接得到一个切片
#执行结果
2017-01-05 00:00:00 0.864041
2017-01-05 12:00:00 0.245966
2017-01-06 00:00:00 0.876838
2017-01-06 12:00:00 0.079419
2017-01-07 00:00:00 0.234074
2017-01-07 12:00:00 0.215010
2017-01-08 00:00:00 0.009161
2017-01-08 12:00:00 0.437424
2017-01-09 00:00:00 0.521541
2017-01-09 12:00:00 0.901745
2017-01-10 00:00:00 0.089922
2017-01-10 12:00:00 0.969074
Freq: 12H, dtype: float64
*******************1********************
2017-02-01 00:00:00 0.013573
2017-02-01 12:00:00 0.201922
2017-02-02 00:00:00 0.487500
2017-02-02 12:00:00 0.000617
2017-02-03 00:00:00 0.796615
Freq: 12H, dtype: float64
# 重复索引的时间序列
dates = pd.DatetimeIndex(['1/1/2015','1/2/2015','1/3/2015','1/4/2015','1/1/2015','1/2/2015'])
ts = pd.Series(np.random.rand(6), index = dates)
print("1".center(40,'*'))
print(ts)
print(ts.is_unique,ts.index.is_unique)
# index有重复,is_unique检查 → values唯一,index不唯一
print("2".center(40,'*'))
print(ts['20150101'],type(ts['20150101']))
print(ts['20150104'],type(ts['20150104']))
# index有重复的将返回多个值
print("3".center(40,'*'))
print(ts.groupby(level = 0).mean())
# 通过groupby做分组,重复的值这里用平均值处理
#执行结果
*******************1********************
2015-01-01 0.135831
2015-01-02 0.412051
2015-01-03 0.936090
2015-01-04 0.304448
2015-01-01 0.396883
2015-01-02 0.056435
dtype: float64
True False
*******************2********************
2015-01-01 0.135831
2015-01-01 0.396883
dtype: float64 <class 'pandas.core.series.Series'>
2015-01-04 0.304448
dtype: float64 <class 'pandas.core.series.Series'>
*******************3********************
2015-01-01 0.266357
2015-01-02 0.234243
2015-01-03 0.936090
2015-01-04 0.304448
dtype: float64