0
0

More than 3 years have passed since last update.

「メモ」pandas:日時でdf

Posted at

カレンダー的データフレームの作製

日付別に日足の経年のデータをまとめる。

sample.py

import pandas as pd
import datetime

day_df = pd.read_pickle("C:/Users/aaaa/Desktop/USDJPY_M1/USDJPY_D1.pickle")

x=pd.DataFrame(columns=['A','B'])#Aに日付、Bに値を入れる予定
for a in range(1,13):
    if a==(1|3|5|7|8|10|12):#3131日月
        for b in range(1,32):
            f = day_df[(day_df.index.month==a) & (day_df.index.day==b)]
            g = f['close']-f['open']
            h= g[g>0].size/g.size
            tmp_se = pd.Series( [ datetime.datetime(2024,a,b),h ], index=x.columns )
            x = x.append(tmp_se, ignore_index=True)
    elif a ==2:#2月は今回は28日とした
        for b in range(1,29):
            f = day_df[(day_df.index.month==a) & (day_df.index.day==b)]
            g = f['close']-f['open']
            h= g[g>0].size/g.size
            tmp_se = pd.Series( [ datetime.datetime(2024,a,b),h ], index=x.columns )
            x = x.append(tmp_se, ignore_index=True)
    else:
        for b in range(1,31):#30日月
            f = day_df[(day_df.index.month==a) & (day_df.index.day==b)]
            g = f['close']-f['open']
            h= g[g>0].size/g.size
            tmp_se = pd.Series( [ datetime.datetime(2024,a,b),h ], index=x.columns )
            x = x.append(tmp_se, ignore_index=True)
x=x.set_index('A')

未達成なこと

今回はカレンダ―作成に年のデータが必要だったので2024年を適当に取ったがこれをなくしたい。クラスを作るか2次元配列で表すのが早そう。ただそうすると日時計算はできなくなるのが難点。まあいらないかな・・・

参考

Pythonのdatetimeで日付や時間と文字列を変換(strftime, strptime)
Python pandas で動的に行を追加するTips(プログラマ向け))

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