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]:
OpenHighLowCloseVolume
Date
2018-09-183310.03350.03260.03345.059100.0
2018-09-193355.03470.03320.03445.0119800.0
2018-09-203465.03510.03445.03490.097500.0
2018-09-213500.03590.03450.03580.0148900.0
2018-09-253605.03775.03570.03730.0128600.0
2018-09-263710.03815.03695.03805.0105200.0
2018-09-273805.03835.03730.03740.083600.0
2018-09-283780.03800.03670.03750.0118100.0
2018-10-013810.03870.03760.03815.0126900.0
2018-10-023805.03855.03745.03805.066500.0
2018-10-033775.03845.03710.03775.065000.0
2018-10-043730.03770.03705.03715.047400.0
2018-10-053645.03750.03645.03740.068200.0
In [3]:
ohlc1['2018-09-26':'2018-09-28'] = 0
ohlc1
Out[3]:
OpenHighLowCloseVolume
Date
2018-09-183310.03350.03260.03345.059100.0
2018-09-193355.03470.03320.03445.0119800.0
2018-09-203465.03510.03445.03490.097500.0
2018-09-213500.03590.03450.03580.0148900.0
2018-09-253605.03775.03570.03730.0128600.0
2018-09-260.00.00.00.00.0
2018-09-270.00.00.00.00.0
2018-09-280.00.00.00.00.0
2018-10-013810.03870.03760.03815.0126900.0
2018-10-023805.03855.03745.03805.066500.0
2018-10-033775.03845.03710.03775.065000.0
2018-10-043730.03770.03705.03715.047400.0
2018-10-053645.03750.03645.03740.068200.0
In [4]:
start2 = datetime(2018, 9, 25)
end2 = datetime(2018, 10, 1)
ohlc2 = get_kabu_data(code, start2, end2)
ohlc2
OpenHighLowCloseVolume
Date
2018-09-253605.03775.03570.03730.0128600.0
2018-09-263710.03815.03695.03805.0105200.0
2018-09-273805.03835.03730.03740.083600.0
2018-09-283780.03800.03670.03750.0118100.0
2018-10-013810.03870.03760.03815.0126900.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]:
OpenHighLowCloseVolume
Date
2018-09-183310.03350.03260.03345.059100.0
2018-09-193355.03470.03320.03445.0119800.0
2018-09-203465.03510.03445.03490.097500.0
2018-09-213500.03590.03450.03580.0148900.0
2018-09-253605.03775.03570.03730.0128600.0
2018-09-263710.03815.03695.03805.0105200.0
2018-09-273805.03835.03730.03740.083600.0
2018-09-283780.03800.03670.03750.0118100.0
2018-10-013810.03870.03760.03815.0126900.0
2018-10-023805.03855.03745.03805.066500.0
2018-10-033775.03845.03710.03775.065000.0
2018-10-043730.03770.03705.03715.047400.0
2018-10-053645.03750.03645.03740.068200.0
2017-09-242000.02010.01999.02005.0100000.0

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

In [6]:
ohlc1.loc[:, 'Open':'Close'] = ohlc1.loc[:, 'Open':'Close']/4
ohlc1.loc[:, 'Volume'] = ohlc1.loc[:, 'Volume']*4
In [7]:
ohlc1
Out[7]:
OpenHighLowCloseVolume
Date
2018-09-18827.50837.50815.00836.25236400.0
2018-09-19838.75867.50830.00861.25479200.0
2018-09-20866.25877.50861.25872.50390000.0
2018-09-21875.00897.50862.50895.00595600.0
2018-09-25901.25943.75892.50932.50514400.0
2018-09-26927.50953.75923.75951.25420800.0
2018-09-27951.25958.75932.50935.00334400.0
2018-09-28945.00950.00917.50937.50472400.0
2018-10-01952.50967.50940.00953.75507600.0
2018-10-02951.25963.75936.25951.25266000.0
2018-10-03943.75961.25927.50943.75260000.0
2018-10-04932.50942.50926.25928.75189600.0
2018-10-05911.25937.50911.25935.00272800.0
2017-09-24500.00502.50499.75501.25400000.0

コメント