1
9

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 1 year has passed since last update.

「Jupyter Notebook」楽天証券の取引履歴CSVから確定申告の「有価証券の内訳書」を集計する備忘録

Last updated at Posted at 2021-04-20

この記事は

また来年も「有価証券の内訳書」を作成するときに使うための備忘録。
(もしニーズがあるようならライブラリに)

やりたかったこと

出来るだけ手作業はしたくなかった

やり方

楽天証券の、マイメニュー > 口座管理 > 取引履歴からCSVをダウンロード

Screen Shot 2023-05-28 at 17.51.11.png

%load_ext nb_black

import numpy as np
import pandas as pd
import glob
import datetime as dt

国内株式

columns = [
    "約定日",
    "受渡日",
    "銘柄コード",
    "銘柄名",
    "市場名称",
    "口座区分",
    "取引区分",
    "売買区分",
    "数量[株]",
    "単価[円]",
    "手数料[円]",
    "税金等[円]",
    "諸費用[円]",
    "税区分",
    "受渡金額[円]",
]

files = glob.glob("*JP*.csv")
csv = []

for filename in files:
    df = pd.read_csv(
        filename,
        encoding="cp932",
        usecols=columns,
        thousands=",",
        parse_dates=["約定日", "受渡日"],
    )
    csv.append(df)

jp_df = pd.concat(csv, axis=0, ignore_index=True)

米国株式

columns = [
    "約定日",
    "受渡日",
    "ティッカー",
    "銘柄名",
    "口座",  # 20220417
    "取引区分",
    "決済通貨",
    "数量[株]",
    "単価[USドル]",
    "約定代金[USドル]",
    "為替レート",
    "手数料[USドル]",
    "税金[USドル]",
    "受渡金額[USドル]",
    "受渡金額[円]",
]

csv = []
df = pd.read_csv(
    "US1.csv",
    encoding="cp932",
    usecols=columns,
    thousands=",",
    parse_dates=["約定日", "受渡日"],
)
csv.append(df)

df = pd.read_csv(
    "US2.csv",
    encoding="cp932",
    usecols=columns,
    thousands=",",
    parse_dates=["約定日", "受渡日"],
)
csv.append(df)
df = pd.concat([jp_df, us_df], axis=0, ignore_index=True)
converted_df = df

trim_hyphens = ["受渡金額[USドル]", "受渡金額[円]", "税金[USドル]", "約定代金[USドル]", "手数料[USドル]"]
for x in trim_hyphens:
    converted_df[x] = np.where(df[x] == "-", 0, df[x])

converted_df["売買区分"] = np.where(df["売買区分"] != df["売買区分"], df["取引区分"], df["売買区分"])
casts = ["受渡金額[USドル]", "約定代金[USドル]", "受渡金額[円]", "税金[USドル]", "手数料[USドル]"]
for x in casts:
    converted_df[x] = df[x].str.replace(",", "").astype(float)

converted_df["受渡金額[円]"] = np.where(df["売買区分"] == "売付", -df["受渡金額[円]"], df["受渡金額[円]"])
converted_df["受渡金額[USドル]"] = np.where(
    df["売買区分"] == "売付", -df["受渡金額[USドル]"], df["受渡金額[USドル]"]
)
converted_df["数量[株]"] = np.where(df["売買区分"] == "売付", -df["数量[株]"], df["数量[株]"])
converted_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 191 entries, 0 to 190
Data columns (total 23 columns):
 #   Column      Non-Null Count  Dtype         
---  ------      --------------  -----         
 0   約定日         191 non-null    datetime64[ns]
 1   受渡日         191 non-null    datetime64[ns]
 2   銘柄コード       81 non-null     float64       
 3   銘柄名         191 non-null    object        
 4   市場名称        81 non-null     object        
 5   口座区分        81 non-null     object        
 6   取引区分        188 non-null    object        
 7   売買区分        191 non-null    object        
 8   数量[株]       191 non-null    int64         
 9   単価[円]       81 non-null     float64       
 10  手数料[円]      81 non-null     float64       
 11  税金等[円]      81 non-null     float64       
 12  諸費用[円]      81 non-null     float64       
 13  税区分         81 non-null     object        
 14  受渡金額[円]     191 non-null    float64       
 15  ティッカー       110 non-null    object        
 16  決済通貨        110 non-null    object        
 17  単価[USドル]    110 non-null    object        
 18  約定代金[USドル]  110 non-null    float64       
 19  為替レート       110 non-null    object        
 20  手数料[USドル]   110 non-null    float64       
 21  税金[USドル]    110 non-null    float64       
 22  受渡金額[USドル]  110 non-null    float64       
dtypes: datetime64[ns](2), float64(10), int64(1), object(10)
memory usage: 34.4+ KB

期末残高の出力

converted_df[converted_df["約定日"] < dt.datetime(2021, 3, 31)].groupby(
    "銘柄名"
).sum().sort_values("数量[株]", ascending=False).to_csv("term_end.csv")

期中増(減)の明細出力

converted_df[
    (converted_df["約定日"] > dt.datetime(2020, 4, 1))
    & (converted_df["約定日"] < dt.datetime(2021, 3, 31))
][["約定日", "銘柄名", "売買区分", "数量[株]", "受渡金額[円]", "受渡金額[USドル]"]].sort_values(
    ["銘柄名", "約定日"]
).to_csv(
    "transactions.csv" # 手動で受渡金額[円]の正負反転が必要
)
1
9
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
1
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?