Data Science | 时间序列的索引与切片

0
0
0
1. 云栖社区>
2. 咸鱼普拉思>
3. 博客>
4. 正文

## Data Science | 时间序列的索引与切片

from datetime import datetime

rng = pd.date_range('2017/1','2017/3')
ts = pd.Series(np.random.rand(len(rng)), index = rng)

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 = rng)
print(ts['2017/1/2'])
print(ts['20170103'])
print(ts['1/10/2017'])
print(ts[datetime(2017,1,20)])
>>>
0.887980757812
0.712861778966
0.788336674948
0.93070380011

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'])
>>>
2017-01-05 00:00:00    0.462085
2017-01-05 12:00:00    0.778637
2017-01-06 00:00:00    0.356306
2017-01-06 12:00:00    0.667964
2017-01-07 00:00:00    0.246857
2017-01-07 12:00:00    0.386956
2017-01-08 00:00:00    0.328203
2017-01-08 12:00:00    0.260853
2017-01-09 00:00:00    0.224920
2017-01-09 12:00:00    0.397457
2017-01-10 00:00:00    0.158729
2017-01-10 12:00:00    0.501266
Freq: 12H, dtype: float64

# 在这里我们可以传入月份可以直接获取整个月份的切片
>>>
2017-02-01 00:00:00    0.243932
2017-02-01 12:00:00    0.220830
2017-02-02 00:00:00    0.896107
2017-02-02 12:00:00    0.476584
2017-02-03 00:00:00    0.515817
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(ts)
# 我们可以通过is_unique检查值或index是否重复
print(ts.is_unique,ts.index.is_unique)
>>>
2015-01-01    0.300286
2015-01-02    0.603865
2015-01-03    0.017949
2015-01-04    0.026621
2015-01-01    0.791441
2015-01-02    0.526622
dtype: float64
True False

print(ts.groupby(level = 0).mean())
# 通过groupby做分组，重复的值这里用平均值处理
>>>
2015-01-01    0.545863
2015-01-02    0.565244
2015-01-03    0.017949
2015-01-04    0.026621
dtype: float64

xianyuplus1995”微信公众号

+ 关注