0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【初心者向け】10行以内のスクリプト(5.pandasを使用した時系列データのresample)

Posted at

【初心者向け】10行以内のスクリプト(5.pandasを使用した時系列データのresample)

pythonで色々なライブラリを使えば、「少ないコードでもちょっとした事が出来て、少なければ5行程でもちょっとしたスクリプトが作れ、便利だな。」と思ったので、pythonやその他、コマンドを羅列しただけのものも出てくるかも知れませんが、10行程のスクリプトを不定期で投稿してみようと思います。

第5回として、pandasを使用した時系列データのresampleを投稿したいと思います。

売上や金融商品の相場、その他、様々な時系列データを月次や四半期毎で、集計したい場面は多いと思います。
pandasのresampleを使えば出来るという事を知り、私自身が忘れないうちに、為替相場のデータを例に年次と四半期で集計する事例を投稿したいと思います。


【環境】
Linux: debian10
python: 3.7.3
pandas: 1.0.3
jupyterlab: 2.1.0

データは、fredの為替相場のデータを使用したいと思います。

以下の様なデータがあったとして

DATEDEXJ,PUS
2015-01-01,NaN
2015-01-02,120.20
2015-01-05,119.64

平均でリサンプルする構文
データフレーム.resample(rule='頻度コード').mean()
・頻度コード: 年次('Y')、四半期('Q')、月次('M')、週次('W')
・例として、「df_dexjpus」というデータフレームの年次の平均を「df_year」というデータフレームに格納する場合
df_year = df_dexjpus.resample(rule='Y').mean()

1. pandasで、為替データを年次・四半期の平均でresampleするスクリプト

コードは、jupyterで実行しました。

python3.7
# 為替相場のリサンプル(年次・四半期・月次・週次)
# 為替相場のデータフレーム(df_dexjpus)
# infile = ('./dexjpus_20200417.csv')
import pandas as pd
import matplotlib.pyplot as plt

df_dexjpus = pd.read_csv('./dexjpus_20200417.csv' ,index_col='DATE' ,parse_dates=['DATE'])

# 年次
df_year = df_dexjpus.resample(rule='Y').mean()

# 2017年のデータ
df_2017 = df_dexjpus.loc['2017-01-01' : '2017-12-31']

# 2017年の四半期の為替の平均
df_quarter = df_2017.resample(rule='Q').mean()    
 

2. スクリプトについて、順番に見ていきたいと思います。

2-1.ライブラリのインポート、データの読む込み

python3.7

# 為替相場のリサンプル(年次・四半期・月次・週次)
# 為替相場のデータフレーム(df_dexjpus)
# infile = ('./dexjpus_20200417.csv')
import pandas as pd
import matplotlib.pyplot as plt

df_dexjpus = pd.read_csv('./dexjpus_20200417.csv' ,index_col='DATE' ,parse_dates=['DATE'])

df_dexjpus


	DEXJPUS
DATE 	
2015-01-01 	NaN
2015-01-02 	120.20
2015-01-05 	119.64
2015-01-06 	118.26
2015-01-07 	119.5

2-2. 1年毎で、見てみようと思います。

python3.7

# 年次
df_year = df_dexjpus.resample(rule='Y').mean()

df_year

	DEXJPUS
DATE 	
2015-12-31 	121.049084
2016-12-31 	108.656932
2017-12-31 	112.098554
2018-12-31 	110.397390
2019-12-31 	109.018835
2020-12-31 	108.775333

2-3. 例として、2017年を見てみようと思います。

python3.7

# 2017年のデータ
df_2017 = df_dexjpus.loc['2017-01-01' : '2017-12-31']

 	DEXJPUS
DATE 	
2017-01-31 	114.872105
2017-02-28 	112.911579
2017-03-31 	112.916522
2017-04-30 	110.091000
2017-05-31 	112.243636
2017-06-30 	110.914091
2017-07-31 	112.417000
2017-08-31 	109.826957
2017-09-30 	110.776000
2017-10-31 	112.914762
2017-11-30 	112.819000
2017-12-31 	112.940500

2-4. 最後、2017年四半期毎のデータを見てみたいと思います。

python3.7

# 2017年の四半期の為替の平均
df_quarter = df_2017.resample(rule='Q').mean()

df_quarter

 	DEXJPUS
DATE 	
2017-03-31 	113.524098
2017-06-30 	111.113906
2017-09-30 	110.950476
2017-12-31 	112.891803

以上、5.pandasを使用した時系列データのresampleでした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?