{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 第3章 pandasでデータを処理しよう\n", "\n", "### 3-9: 時系列データの処理" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TOEI ANIMATIONIG Port
Date
2015-01-013356.861201.51
2015-01-023356.861201.51
2015-01-053396.121218.44
2015-01-063361.771201.51
2015-01-073297.971202.51
\n", "
" ], "text/plain": [ " TOEI ANIMATION IG Port\n", "Date \n", "2015-01-01 3356.86 1201.51\n", "2015-01-02 3356.86 1201.51\n", "2015-01-05 3396.12 1218.44\n", "2015-01-06 3361.77 1201.51\n", "2015-01-07 3297.97 1202.51" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.1:株価の取得\n", "import os\n", "import pandas as pd\n", "\n", "base_url = (\n", " \"https://raw.githubusercontent.com/practical-jupyter/sample-data/master/anime/\"\n", ")\n", "anime_stock_price_csv = os.path.join(base_url, \"anime_stock_price.csv\")\n", "df = pd.read_csv(anime_stock_price_csv, index_col=0, parse_dates=[\"Date\"])\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Date\n", "2015-01-01 NaN\n", "2015-01-02 0.000000\n", "2015-01-05 0.011695\n", "2015-01-06 -0.010114\n", "2015-01-07 -0.018978\n", " ... \n", "2016-12-26 0.001725\n", "2016-12-27 0.010326\n", "2016-12-28 0.015333\n", "2016-12-29 0.013422\n", "2016-12-30 0.000000\n", "Name: TOEI ANIMATION, dtype: float64" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.2:騰落率\n", "pd.options.display.max_rows = 10 # pandasで表示する行数を設定\n", "pct_change = df[\"TOEI ANIMATION\"].pct_change()\n", "pct_change" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Date\n", "2015-01-01 1.000000\n", "2015-01-02 1.000000\n", "2015-01-05 1.011695\n", "2015-01-06 1.001463\n", "2015-01-07 0.982457\n", " ... \n", "2016-12-26 1.722833\n", "2016-12-27 1.740624\n", "2016-12-28 1.767312\n", "2016-12-29 1.791034\n", "2016-12-30 1.791034\n", "Name: TOEI ANIMATION, dtype: float64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.4:累積リターン\n", "cumulative_returns = (pct_change + 1).cumprod()\n", "cumulative_returns[0] = 1\n", "cumulative_returns" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Date\n", "2015-01-01 NaN\n", "2015-01-02 NaN\n", "2015-01-05 NaN\n", "2015-01-06 NaN\n", "2015-01-07 3353.916\n", " ... \n", "2016-12-26 5793.260\n", "2016-12-27 5799.232\n", "2016-12-28 5821.132\n", "2016-12-29 5868.912\n", "2016-12-30 5916.692\n", "Name: TOEI ANIMATION, dtype: float64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.6:移動平均\n", "df[\"TOEI ANIMATION\"].rolling(5).mean()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Date\n", "2015-01-01 NaN\n", "2015-01-02 NaN\n", "2015-01-05 NaN\n", "2015-01-06 NaN\n", "2015-01-07 NaN\n", " ... \n", "2016-12-26 0.158355\n", "2016-12-27 0.163235\n", "2016-12-28 0.170891\n", "2016-12-29 0.172681\n", "2016-12-30 0.151983\n", "Name: TOEI ANIMATION, dtype: float64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.8:20日間のヒストリカルボラティリティ\n", "import numpy as np\n", "\n", "\n", "def historical_volatility(x):\n", " logreturns = np.diff(np.log(x)) # 対数収益率\n", " return np.sqrt(365 * logreturns.var())\n", "\n", "\n", "df[\"TOEI ANIMATION\"].rolling(20).apply(historical_volatility)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 01:00:00',\n", " '2017-01-01 02:00:00', '2017-01-01 03:00:00',\n", " '2017-01-01 04:00:00', '2017-01-01 05:00:00',\n", " '2017-01-01 06:00:00', '2017-01-01 07:00:00',\n", " '2017-01-01 08:00:00', '2017-01-01 09:00:00',\n", " ...\n", " '2017-01-31 15:00:00', '2017-01-31 16:00:00',\n", " '2017-01-31 17:00:00', '2017-01-31 18:00:00',\n", " '2017-01-31 19:00:00', '2017-01-31 20:00:00',\n", " '2017-01-31 21:00:00', '2017-01-31 22:00:00',\n", " '2017-01-31 23:00:00', '2017-02-01 00:00:00'],\n", " dtype='datetime64[ns]', length=745, freq='H')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.10:DatetimeIndexの作成\n", "ix = pd.date_range(\"2017-01\", \"2017-02\", freq=\"1H\")\n", "ix" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2017-01-01 00:00:00 0\n", "2017-01-01 01:00:00 1\n", "2017-01-01 02:00:00 2\n", "2017-01-01 03:00:00 3\n", "2017-01-01 04:00:00 4\n", " ... \n", "2017-01-31 20:00:00 740\n", "2017-01-31 21:00:00 741\n", "2017-01-31 22:00:00 742\n", "2017-01-31 23:00:00 743\n", "2017-02-01 00:00:00 744\n", "Freq: H, dtype: int64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.12:DatetimeIndexを使用したSeries\n", "time_series = pd.Series(np.arange(len(ix)), index=ix)\n", "time_series" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "TOEI ANIMATION 5699.74\n", "IG Port 822.66\n", "Name: 2016-01-04 00:00:00, dtype: float64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.14:インデクサにdatetime型の値を指定\n", "from datetime import datetime\n", "\n", "df.loc[datetime(2016, 1, 4)]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "TOEI ANIMATION 5699.74\n", "IG Port 822.66\n", "Name: 2016-01-04 00:00:00, dtype: float64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.16:インデクサに日付形式の文字列を指定①\n", "df.loc[\"2016-01-04\"]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "TOEI ANIMATION 5699.74\n", "IG Port 822.66\n", "Name: 2016-01-04 00:00:00, dtype: float64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.17:インデクサに日付形式の文字列を指定②\n", "df.loc[\"Jan-04-2016\"]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " TOEI ANIMATION IG Port\n", "Date \n", "2015-01-01 3356.86 1201.51\n", "2015-01-02 3356.86 1201.51\n", "2015-01-05 3396.12 1218.44\n", "2015-01-06 3361.77 1201.51\n", "2015-01-07 3297.97 1202.51\n" ] } ], "source": [ "# リスト3.9.18:年を指定した抽出\n", "print(df.loc[\"2015\"].head())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " TOEI ANIMATION IG Port\n", "Date \n", "2016-05-02 4703.29 933.34\n", "2016-05-03 4703.29 933.34\n", "2016-05-04 4703.29 933.34\n", "2016-05-05 4703.29 933.34\n", "2016-05-06 4678.41 953.28\n" ] } ], "source": [ "# リスト3.9.20:月を指定した抽出\n", "print(df.loc[\"2016-05\"].head())" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " TOEI ANIMATION IG Port\n", "Date \n", "2015-12-01 5947.13 910.41\n", "2015-12-02 5917.44 896.45\n", "2015-12-03 5917.44 893.46\n", "2015-12-04 5867.97 888.47\n", "2015-12-07 5917.44 892.46\n", "... ... ...\n", "2016-01-25 5452.36 704.99\n", "2016-01-26 5491.94 702.00\n", "2016-01-27 5521.63 721.94\n", "2016-01-28 5679.95 733.91\n", "2016-01-29 5670.06 739.89\n", "\n", "[44 rows x 2 columns]\n" ] } ], "source": [ "# リスト3.9.22:年月でスライス\n", "print(df.loc[\"2015-12\":\"2016-01\"])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2017-01-01 09:00:00 9\n", "2017-01-02 09:00:00 33\n", "2017-01-03 09:00:00 57\n", "2017-01-04 09:00:00 81\n", "2017-01-05 09:00:00 105\n", " ... \n", "2017-01-27 09:00:00 633\n", "2017-01-28 09:00:00 657\n", "2017-01-29 09:00:00 681\n", "2017-01-30 09:00:00 705\n", "2017-01-31 09:00:00 729\n", "Freq: 24H, dtype: int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.24:9時のデータを抽出\n", "from datetime import time\n", "\n", "time_series.loc[time(9, 0)]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2017-01-01 09:00:00 9\n", "2017-01-01 10:00:00 10\n", "2017-01-01 11:00:00 11\n", "2017-01-01 12:00:00 12\n", "2017-01-02 09:00:00 33\n", " ... \n", "2017-01-30 12:00:00 708\n", "2017-01-31 09:00:00 729\n", "2017-01-31 10:00:00 730\n", "2017-01-31 11:00:00 731\n", "2017-01-31 12:00:00 732\n", "dtype: int64" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.26:9時から12時のデータを抽出\n", "time_series.between_time(time(9, 0), time(12, 0))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Date\n", "2015-01-31 3647.080000\n", "2015-02-28 3612.302500\n", "2015-03-31 3625.770455\n", "2015-04-30 3477.555455\n", "2015-05-31 3653.990476\n", "Freq: M, Name: TOEI ANIMATION, dtype: float64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.28:月次の株価の平均値\n", "df[\"TOEI ANIMATION\"].resample(\"M\").mean().head()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
openhighlowclose
Date
2015-01-043356.863356.863356.863356.86
2015-01-113396.123513.903297.973513.90
2015-01-183513.903872.163435.383872.16
2015-01-253877.073877.073739.663739.66
2015-02-013774.013965.413774.013965.41
\n", "
" ], "text/plain": [ " open high low close\n", "Date \n", "2015-01-04 3356.86 3356.86 3356.86 3356.86\n", "2015-01-11 3396.12 3513.90 3297.97 3513.90\n", "2015-01-18 3513.90 3872.16 3435.38 3872.16\n", "2015-01-25 3877.07 3877.07 3739.66 3739.66\n", "2015-02-01 3774.01 3965.41 3774.01 3965.41" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト3.9.30:日次の終値を週足に変換\n", "df[\"TOEI ANIMATION\"].resample(\"W\").ohlc().head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }