时间序列的索引与重采样

Python 中的时间序列数据处理

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

时间序列变换

基础时间序列变换包括:

  • 解析字符串日期并转为 datetime64

  • 选择与切片特定子时期

  • 设定与更改 DateTimeIndex 频率

    • 上采样 vs 下采样
Python 中的时间序列数据处理

获取 GOOG 股价

google = pd.read_csv('google.csv')  # import pandas as pd

google.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 504 entries, 0 to 503
Data columns (total 2 columns):
date     504 non-null object
price    504 non-null float64
dtypes: float64(1), object(1)
google.head()
         date   price
0  2015-01-02  524.81
1  2015-01-05  513.87
2  2015-01-06  501.96
3  2015-01-07  501.10
4  2015-01-08  502.68
Python 中的时间序列数据处理

将字符串日期转为 datetime64

  • pd.to_datetime()
    • 解析日期字符串
    • 转为 datetime64
google.date = pd.to_datetime(google.date)

google.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 504 entries, 0 to 503
Data columns (total 2 columns):
date     504 non-null datetime64[ns]
price    504 non-null float64
dtypes: datetime64[ns](1), float64(1)
Python 中的时间序列数据处理

将字符串日期转为 datetime64

  • .set_index()
    • 将日期设为索引
    • inplace
      • 不创建副本
google.set_index('date', inplace=True)

google.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 504 entries, 2015-01-02 to 2016-12-30
Data columns (total 1 columns):
price    504 non-null float64
dtypes: float64(1)
Python 中的时间序列数据处理

绘制谷歌股价时间序列

google.price.plot(title='Google Stock Price')

plt.tight_layout(); plt.show()

谷歌股价时间序列图

Python 中的时间序列数据处理

部分字符串索引

  • 使用可解析为日期的字符串进行选择/索引
google['2015'].info() # 传入表示部分日期的字符串
DatetimeIndex: 252 entries, 2015-01-02 to 2015-12-31
Data columns (total 1 columns):
price    252 non-null float64
dtypes: float64(1)
google['2015-3': '2016-2'].info() # 切片包含最后一个月份
DatetimeIndex: 252 entries, 2015-03-02 to 2016-02-29
Data columns (total 1 columns):
price    252 non-null float64
dtypes: float64(1)
memory usage: 3.9 KB
Python 中的时间序列数据处理

部分字符串索引

google.loc['2016-6-1', 'price'] # 用完整日期配合 .loc[]
734.15
Python 中的时间序列数据处理

.asfreq(): 设定频率

  • .asfreq('D'):
    • DateTimeIndex 转为日历日频率
google.asfreq('D').info() # 设为日历日频率
DatetimeIndex: 729 entries, 2015-01-02 to 2016-12-30
Freq: D
Data columns (total 1 columns):
price    504 non-null float64
dtypes: float64(1)
Python 中的时间序列数据处理

.asfreq(): 设定频率

  • 上采样:
    • 更高频率会引入新日期 => 缺失值
google.asfreq('D').head()
             price
date              
2015-01-02  524.81
2015-01-03     NaN
2015-01-04     NaN
2015-01-05  513.87
2015-01-06  501.96
Python 中的时间序列数据处理

.asfreq(): 重设频率

  • .asfreq('B'):
    • DateTimeIndex 转为工作日频率
google = google.asfreq('B') # 改为日历日频率

google.info()
DatetimeIndex: 521 entries, 2015-01-02 to 2016-12-30
Freq: B
Data columns (total 1 columns):
price    504 non-null float64
dtypes: float64(1)
Python 中的时间序列数据处理

.asfreq(): 重设频率

google[google.price.isnull()] # 选择缺失的 'price'
            price
date             
2015-01-19    NaN
2015-02-16    NaN
...
2016-11-24    NaN
2016-12-26    NaN
  • 非交易的工作日
Python 中的时间序列数据处理

Passons à la pratique !

Python 中的时间序列数据处理

Preparing Video For Download...