Skip to content

Instantly share code, notes, and snippets.

@orangain
Created September 15, 2019 08:07
Show Gist options
  • Save orangain/5e38a9dbd009af9da3a183408e7245c3 to your computer and use it in GitHub Desktop.
Save orangain/5e38a9dbd009af9da3a183408e7245c3 to your computer and use it in GitHub Desktop.
pandasで株式分割・併合に伴う株価調整
import pandas as pd
dfs = pd.read_html('https://srbrnote.work/archives/2338')
df_splits = dfs[0] # 株式分割・併合データ
df_stocks = dfs[2] # 株価データ
df_splits['分割日'] = pd.to_datetime(df_splits['分割日'])
df_splits['係数'] = df_splits['分割前'] / df_splits['分割後']
df_stocks['日付'] = pd.to_datetime(df_stocks['日付'])
# 日付ごとの係数を計算
def coefficient(date, split):
if date < split['分割日']:
return split['係数']
else:
return 1
df_coefficients = pd.DataFrame(df_stocks['日付'])
df_coefficients['係数'] = 1
for _, split in df_splits.iterrows():
df_coefficients['係数'] *= df_coefficients.apply(lambda c: coefficient(c['日付'], split), axis=1)
# 株価に係数を適用
df_stocks['始値'] = df_stocks['始値'] * df_coefficients['係数']
df_stocks['高値'] = df_stocks['高値'] * df_coefficients['係数']
df_stocks['安値'] = df_stocks['安値'] * df_coefficients['係数']
df_stocks['終値'] = df_stocks['終値'] * df_coefficients['係数']
print(df_stocks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment