pandasによる株価データベースのアップデート

Pandas
In [1]:
from get_kabu_data import get_kabu_data
from datetime import datetime
import pandas as pd
code = '4348'
start1 = datetime(2018, 9, 15)
end1 = datetime(2018, 10, 5)
ohlc1 = get_kabu_data(code, start1, end1)
In [2]:
ohlc1
Out[2]:
Open High Low Close Volume
Date
2018-09-18 3310.0 3350.0 3260.0 3345.0 59100.0
2018-09-19 3355.0 3470.0 3320.0 3445.0 119800.0
2018-09-20 3465.0 3510.0 3445.0 3490.0 97500.0
2018-09-21 3500.0 3590.0 3450.0 3580.0 148900.0
2018-09-25 3605.0 3775.0 3570.0 3730.0 128600.0
2018-09-26 3710.0 3815.0 3695.0 3805.0 105200.0
2018-09-27 3805.0 3835.0 3730.0 3740.0 83600.0
2018-09-28 3780.0 3800.0 3670.0 3750.0 118100.0
2018-10-01 3810.0 3870.0 3760.0 3815.0 126900.0
2018-10-02 3805.0 3855.0 3745.0 3805.0 66500.0
2018-10-03 3775.0 3845.0 3710.0 3775.0 65000.0
2018-10-04 3730.0 3770.0 3705.0 3715.0 47400.0
2018-10-05 3645.0 3750.0 3645.0 3740.0 68200.0
In [3]:
ohlc1['2018-09-26':'2018-09-28'] = 0
ohlc1
Out[3]:
Open High Low Close Volume
Date
2018-09-18 3310.0 3350.0 3260.0 3345.0 59100.0
2018-09-19 3355.0 3470.0 3320.0 3445.0 119800.0
2018-09-20 3465.0 3510.0 3445.0 3490.0 97500.0
2018-09-21 3500.0 3590.0 3450.0 3580.0 148900.0
2018-09-25 3605.0 3775.0 3570.0 3730.0 128600.0
2018-09-26 0.0 0.0 0.0 0.0 0.0
2018-09-27 0.0 0.0 0.0 0.0 0.0
2018-09-28 0.0 0.0 0.0 0.0 0.0
2018-10-01 3810.0 3870.0 3760.0 3815.0 126900.0
2018-10-02 3805.0 3855.0 3745.0 3805.0 66500.0
2018-10-03 3775.0 3845.0 3710.0 3775.0 65000.0
2018-10-04 3730.0 3770.0 3705.0 3715.0 47400.0
2018-10-05 3645.0 3750.0 3645.0 3740.0 68200.0
In [4]:
start2 = datetime(2018, 9, 25)
end2 = datetime(2018, 10, 1)
ohlc2 = get_kabu_data(code, start2, end2)
ohlc2
Open High Low Close Volume
Date
2018-09-25 3605.0 3775.0 3570.0 3730.0 128600.0
2018-09-26 3710.0 3815.0 3695.0 3805.0 105200.0
2018-09-27 3805.0 3835.0 3730.0 3740.0 83600.0
2018-09-28 3780.0 3800.0 3670.0 3750.0 118100.0
2018-10-01 3810.0 3870.0 3760.0 3815.0 126900.0
In [5]:
for i in ohlc2.index:
    
    if i in ohlc1.index:
    
        check = ohlc2.loc[i].equals(ohlc1.loc[i])
        print(i,check)
        if not check:
            ohlc1.loc[i] = ohlc2.loc[i]
    else:
        ohlc1.loc[i] = ohlc2.loc[i]

if not ('2017-09-24' in ohlc1.index):
    ohlc1.loc['2017-09-24'] = [2000.0,2010.0,1999.0,2005.0,100000]
ohlc1
2018-09-25 True
2018-09-26 False
2018-09-27 False
2018-09-28 False
2018-10-01 True
Out[5]:
Open High Low Close Volume
Date
2018-09-18 3310.0 3350.0 3260.0 3345.0 59100.0
2018-09-19 3355.0 3470.0 3320.0 3445.0 119800.0
2018-09-20 3465.0 3510.0 3445.0 3490.0 97500.0
2018-09-21 3500.0 3590.0 3450.0 3580.0 148900.0
2018-09-25 3605.0 3775.0 3570.0 3730.0 128600.0
2018-09-26 3710.0 3815.0 3695.0 3805.0 105200.0
2018-09-27 3805.0 3835.0 3730.0 3740.0 83600.0
2018-09-28 3780.0 3800.0 3670.0 3750.0 118100.0
2018-10-01 3810.0 3870.0 3760.0 3815.0 126900.0
2018-10-02 3805.0 3855.0 3745.0 3805.0 66500.0
2018-10-03 3775.0 3845.0 3710.0 3775.0 65000.0
2018-10-04 3730.0 3770.0 3705.0 3715.0 47400.0
2018-10-05 3645.0 3750.0 3645.0 3740.0 68200.0
2017-09-24 2000.0 2010.0 1999.0 2005.0 100000.0

株式分割に対応するには?

In [6]:
ohlc1.loc[:, 'Open':'Close'] = ohlc1.loc[:, 'Open':'Close']/4
ohlc1.loc[:, 'Volume'] = ohlc1.loc[:, 'Volume']*4
In [7]:
ohlc1
Out[7]:
Open High Low Close Volume
Date
2018-09-18 827.50 837.50 815.00 836.25 236400.0
2018-09-19 838.75 867.50 830.00 861.25 479200.0
2018-09-20 866.25 877.50 861.25 872.50 390000.0
2018-09-21 875.00 897.50 862.50 895.00 595600.0
2018-09-25 901.25 943.75 892.50 932.50 514400.0
2018-09-26 927.50 953.75 923.75 951.25 420800.0
2018-09-27 951.25 958.75 932.50 935.00 334400.0
2018-09-28 945.00 950.00 917.50 937.50 472400.0
2018-10-01 952.50 967.50 940.00 953.75 507600.0
2018-10-02 951.25 963.75 936.25 951.25 266000.0
2018-10-03 943.75 961.25 927.50 943.75 260000.0
2018-10-04 932.50 942.50 926.25 928.75 189600.0
2018-10-05 911.25 937.50 911.25 935.00 272800.0
2017-09-24 500.00 502.50 499.75 501.25 400000.0

コメント