Pythonでの時系列データの扱い5 〜 タイムスタンプから期間への変換

Pythonでの時系列データの扱い4 〜 「祝日の取得」および「祝日を考慮した営業日の取得」」の続き。

結論から言うと、今のところこの機能を活かす状況が分かっていないので、軽く流す。
次の流れで進める。

1.pd.date_range()でDatetimeIndexを作成する
2.1で作成したDatetimeIndexをpandas.Seriesのindexに指定して時系列データを作成する
3.2のデータを期間へ変換する
4.3のデータを期間からタイムスタンプへ変換する

コードは次。

#coding:utf-8
import pandas as pd
import numpy as np

## 1. pandas.date_rangeを使用してタイムスタンプインデックスを作成する ##
dataNum=20
idx_ = pd.date_range('1/1/2000', periods=dataNum, freq='D')
print "1. pandas.date_rangeを使用してタイムスタンプインデックスを作成する"
print idx_

## 2. 時系列データを作成 ##
ts = pd.Series(np.random.rand(dataNum), index=idx_)
print "2. 時系列データを作成"
print ts

## 3. 2のデータを期間へ変換する ##
pts = ts.to_period('M')
print "3. 2のデータを期間へ変換する"
print "月間隔"
print pts

pts = ts.to_period('D')
print "日間隔"
print pts

## 4. 3のデータを期間からタイムスタンプへ変換する(データが戻るわけではない) ##
# 1ヶ月間隔に変換
pts = ts.to_period('M')
print "4. 3のデータを期間からタイムスタンプへ変換する(データが戻るわけではない)"
print "# 1ヶ月間隔に変換"
print pts

# 1日間隔に変換
print "# 1日間隔に変換"
print pts.to_timestamp('D')

# 月末表示に変換
print "# 月末表示に変換"
print pts.to_timestamp('M', how='end')

実行結果は次。

1. pandas.date_rangeを使用してタイムスタンプインデックスを作成する
DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04',
'2000-01-05', '2000-01-06', '2000-01-07', '2000-01-08',
'2000-01-09', '2000-01-10', '2000-01-11', '2000-01-12',
'2000-01-13', '2000-01-14', '2000-01-15', '2000-01-16',
'2000-01-17', '2000-01-18', '2000-01-19', '2000-01-20'],
dtype='datetime64[ns]', freq='D')
2. 時系列データを作成
2000-01-01 0.518883
2000-01-02 0.834827
2000-01-03 0.722651
2000-01-04 0.832670
2000-01-05 0.810714
2000-01-06 0.893790
2000-01-07 0.268938
2000-01-08 0.609832
2000-01-09 0.345413
2000-01-10 0.500000
2000-01-11 0.460510
2000-01-12 0.402549
2000-01-13 0.666952
2000-01-14 0.065596
2000-01-15 0.370893
2000-01-16 0.896377
2000-01-17 0.145320
2000-01-18 0.730123
2000-01-19 0.006006
2000-01-20 0.553073
Freq: D, dtype: float64
3. 2のデータを期間へ変換する
月間隔
2000-01 0.518883
2000-01 0.834827
2000-01 0.722651
2000-01 0.832670
2000-01 0.810714
2000-01 0.893790
2000-01 0.268938
2000-01 0.609832
2000-01 0.345413
2000-01 0.500000
2000-01 0.460510
2000-01 0.402549
2000-01 0.666952
2000-01 0.065596
2000-01 0.370893
2000-01 0.896377
2000-01 0.145320
2000-01 0.730123
2000-01 0.006006
2000-01 0.553073
Freq: M, dtype: float64
日間隔
2000-01-01 0.518883
2000-01-02 0.834827
2000-01-03 0.722651
2000-01-04 0.832670
2000-01-05 0.810714
2000-01-06 0.893790
2000-01-07 0.268938
2000-01-08 0.609832
2000-01-09 0.345413
2000-01-10 0.500000
2000-01-11 0.460510
2000-01-12 0.402549
2000-01-13 0.666952
2000-01-14 0.065596
2000-01-15 0.370893
2000-01-16 0.896377
2000-01-17 0.145320
2000-01-18 0.730123
2000-01-19 0.006006
2000-01-20 0.553073
Freq: D, dtype: float64
Backend TkAgg is interactive backend. Turning interactive mode on.
4. 3のデータを期間からタイムスタンプへ変換する(データが戻るわけではない)
# 1ヶ月間隔に変換
2000-01 0.518883
2000-01 0.834827
2000-01 0.722651
2000-01 0.832670
2000-01 0.810714
2000-01 0.893790
2000-01 0.268938
2000-01 0.609832
2000-01 0.345413
2000-01 0.500000
2000-01 0.460510
2000-01 0.402549
2000-01 0.666952
2000-01 0.065596
2000-01 0.370893
2000-01 0.896377
2000-01 0.145320
2000-01 0.730123
2000-01 0.006006
2000-01 0.553073
Freq: M, dtype: float64
# 1日間隔に変換
2000-01-01 0.518883
2000-01-01 0.834827
2000-01-01 0.722651
2000-01-01 0.832670
2000-01-01 0.810714
2000-01-01 0.893790
2000-01-01 0.268938
2000-01-01 0.609832
2000-01-01 0.345413
2000-01-01 0.500000
2000-01-01 0.460510
2000-01-01 0.402549
2000-01-01 0.666952
2000-01-01 0.065596
2000-01-01 0.370893
2000-01-01 0.896377
2000-01-01 0.145320
2000-01-01 0.730123
2000-01-01 0.006006
2000-01-01 0.553073
dtype: float64
# 月末表示に変換
2000-01-31 0.518883
2000-01-31 0.834827
2000-01-31 0.722651
2000-01-31 0.832670
2000-01-31 0.810714
2000-01-31 0.893790
2000-01-31 0.268938
2000-01-31 0.609832
2000-01-31 0.345413
2000-01-31 0.500000
2000-01-31 0.460510
2000-01-31 0.402549
2000-01-31 0.666952
2000-01-31 0.065596
2000-01-31 0.370893
2000-01-31 0.896377
2000-01-31 0.145320
2000-01-31 0.730123
2000-01-31 0.006006
2000-01-31 0.553073
dtype: float64

Process finished with exit code 0

以上。

Pythonによるデータ分析入門 ―NumPy、pandasを使ったデータ処理

Pythonによるデータ分析入門 ―NumPy、pandasを使ったデータ処理