Python--时间序列.s(索引、切片、重采样)

时间序列    索引 / 切片 / 重采样


时间序列  👉 索引

 

# 示例数据
import numpy as np
import pandas as pd
import datetime

times = pd.date_range('2019-1-1',periods=10,freq='MS')
ps = pd.Series(np.random.rand(len(times)),index=times)

#-----输出-----#
2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
2019-04-01    0.509028
2019-05-01    0.943419
2019-06-01    0.619618
2019-07-01    0.736451
2019-08-01    0.695320
2019-09-01    0.607416
2019-10-01    0.506309
Freq: MS, dtype: float64

索引 (整数索引,索引和列表一样没有区别。) : 

ps[0]
ps[::2]
ps[:3]
ps['2019']
ps['2019-1']

#-----输出-----#
0.3741804976952492

2019-01-01    0.374180
2019-03-01    0.098253
2019-05-01    0.943419
2019-07-01    0.736451
2019-09-01    0.607416

2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
Freq: MS, dtype: float64

2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
2019-04-01    0.509028
2019-05-01    0.943419
2019-06-01    0.619618
2019-07-01    0.736451
2019-08-01    0.695320
2019-09-01    0.607416
2019-10-01    0.506309
Freq: MS, dtype: float64

2019-01-01    0.37418
Freq: MS, dtype: float64
View Code
[datetime.datetime(2019,1,1)]

#-----输出-----#
0.3741804976952492

👆 时间序列索引,支持各种字符串datetime 对象

 


时间序列 👉 切片

 

ps['2019-1':'2019-5']
ps['2019-1':'2019-5':2]

#-----输出-----#
2019-01-01    0.374180
2019-02-01    0.354294
2019-03-01    0.098253
2019-04-01    0.509028
2019-05-01    0.943419
Freq: MS, dtype: float64

2019-01-01    0.374180
2019-03-01    0.098253
2019-05-01    0.943419
Freq: 2MS, dtype: float64

 

👆 切片 和 Series索引标签切片原理一致,是一个闭区间,包头尾

 

 


重复索引的时间序列

 

tid = pd.DatetimeIndex(['2019-10-1','2019-10-2','2019-10-2','2019-10-4','2019-10-5','2019-10-6'])
ps = pd.Series(np.random.rand(len(tid)),index=tid)

#-----输出-----#
2019-10-01    0.740345
2019-10-02    0.087693
2019-10-02    0.710417
2019-10-04    0.140575
2019-10-05    0.834221
2019-10-06    0.312474
dtype: float64

 

👇 .is_unique : 检查值是否唯一  // \\ .index.is_unique : 检查索引是否唯一 

ps.index.is_unique
#-----输出----#
False

ps.is_unique
#-----输出----#
True

# 如果索引值重复,则返回多个值 ,如下 :

ps['2019-10-2']

#-----输出-----#
2019-10-02    0.087693
2019-10-02    0.710417
dtype: float64

 


时间序列 👉 重采样   .resample()

 

将时间序列从一个频率转换为另一个频率的过程

 

降采样 , 高频数据-->低频数据

 

升采样 , 低频数据-->高频数据

# 示例数据
rs = pd.date_range('2019-1-1',periods=12,freq="MS")
ts = pd.Series(np.arange(len(rs)),index=rs)

#-----输出-----#
2019-01-01     0
2019-02-01     1
2019-03-01     2
2019-04-01     3
2019-05-01     4
2019-06-01     5
2019-07-01     6
2019-08-01     7
2019-09-01     8
2019-10-01     9
2019-11-01    10
2019-12-01    11
Freq: MS, dtype: int32

每2个月进行值求和 : 

ts = ts.resample('2MS').sum()  

#-----输出-----#
2019-01-01     1
2019-03-01     5
2019-05-01     9
2019-07-01    13
2019-09-01    17
2019-11-01    21
Freq: 2MS, dtype: int32

##  生成一个重采样的构建器 ,频率改为2MS 2个月初
#  .sum() 得到一个重新聚合后的Series,聚合方式时求和
#  降采样,需要聚合
#  mean() / max() / min() / median() 中值 / first() / last() / ohlc() 重采样
#  OHLC : 金融领域的时间序列聚合方式 → open 开盘 high 最大值 low 最小值 close 收盘

ts .resample('2MS',closed='right').sum()  # 每隔2行数据 , 下2个日期相加之和
ts.resample('2MS',closed='left').sum()  #每隔2行数据,当前值+下一个值之和
#具体作用 我是新手 我也不懂

#-----输出-----#
2018-11-01     0
2019-01-01     3
2019-03-01     7
2019-05-01    11
2019-07-01    15
2019-09-01    19
2019-11-01    11
Freq: 2MS, dtype: int32

2019-01-01     1
2019-03-01     5
2019-05-01     9
2019-07-01    13
2019-09-01    17
2019-11-01    21
Freq: 2MS, dtype: int32

 升采样

ts.resample('H')

# 生成一个重采样的构建器
# 从低频转高频,主要是如何插值

DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, label=left, convention=start, base=0]

ts.resample('H').ffill()   # .ffill() 向前填充
ts.resample('H').bfill()   # .bfill() 向后填充
ts.resample('H').asfreq()   # .asfreq() 不做填充 返回NaN

# 值填充  数据太长了 , 不展示 #

时期的重采样 :

pp = pd.period_range('2018','2019',freq='M')
ts = pd.Series(np.arange(len(pp)),index=pp)

#-----示例-----#
2018-01     0
2018-02     1
2018-03     2
2018-04     3
2018-05     4
2018-06     5
2018-07     6
2018-08     7
2018-09     8
2018-10     9
2018-11    10
2018-12    11
2019-01    12
Freq: M, dtype: int32

对于时期的降采样,目标频率要是原频率的超时期 :

ts.resample('Q').sum()  #降采样
ts.resample('D').ffill()   #升采样

运行结果

2018Q1     3
2018Q2    12
2018Q3    21
2018Q4    30
2019Q1    12
Freq: Q-DEC, dtype: int32

2018-01-01     0
2018-01-02     0
2018-01-03     0
2018-01-04     0
2018-01-05     0
2018-01-06     0
2018-01-07     0
2018-01-08     0
2018-01-09     0
2018-01-10     0
2018-01-11     0
2018-01-12     0
2018-01-13     0
2018-01-14     0
2018-01-15     0
2018-01-16     0
2018-01-17     0
2018-01-18     0
2018-01-19     0
2018-01-20     0
2018-01-21     0
2018-01-22     0
2018-01-23     0
2018-01-24     0
2018-01-25     0
2018-01-26     0
2018-01-27     0
2018-01-28     0
2018-01-29     0
2018-01-30     0
              ..
2019-01-02    12
2019-01-03    12
2019-01-04    12
2019-01-05    12
2019-01-06    12
2019-01-07    12
2019-01-08    12
2019-01-09    12
2019-01-10    12
2019-01-11    12
2019-01-12    12
2019-01-13    12
2019-01-14    12
2019-01-15    12
2019-01-16    12
2019-01-17    12
2019-01-18    12
2019-01-19    12
2019-01-20    12
2019-01-21    12
2019-01-22    12
2019-01-23    12
2019-01-24    12
2019-01-25    12
2019-01-26    12
2019-01-27    12
2019-01-28    12
2019-01-29    12
2019-01-30    12
2019-01-31    12
Freq: D, Length: 396, dtype: int32
View Code

## 我也不懂,请自行 “百度” ## 

 

 

 

 

posted @ 2019-11-08 22:22  逍遥大帝  阅读(3054)  评论(0编辑  收藏  举报