時間序列 索引 / 切片 / 重采樣
時間序列 👉 索引
# 示例數據 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
[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
## 我也不懂,請自行 “百度” ##