時系列のインデックス化とリサンプリング

Pythonでの時系列データ操作

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

時系列の変換

基本的な時系列変換:

  • 文字列日付を解析し datetime64 に変換

  • 特定期間の選択・スライス

  • DateTimeIndex の頻度設定・変更

    • アップサンプリングとダウンサンプリング
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株の時系列をプロット

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

plt.tight_layout(); plt.show()

ch1_2_v2 - Indexing & Resampling Time Series.013.png

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...