{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Data Set Construction\n", "\n", "**Functions**\n", "\n", "`pd.read_csv`, `pd.read_excel`, `np.diff` or `DataFrame.diff`, `DataFrame.resample`\n", "\n", "### Exercise 1\n", "\n", "1. Download all available daily data for the S&P 500 and the Hang Seng Index from Yahoo! Finance. \n", "2. Import both data sets into Python. The final dataset should have a `DateTimeIndex`, and the date\n", " column should not be part of the `DataFrame`.\n", "3. Construct weekly price series from each, using Tuesday prices (less likely to be a holiday).\n", "4. Construct monthly price series from each using last day in the month.\n", "5. Save the data to the HDF file \"equity-indices.h5\".\n" ] }, { "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", " \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", "
OpenHighLowCloseAdj CloseVolume
Date
2019-09-172995.6699223006.2099612993.7299803005.6999513005.6999513671840000
2019-09-183001.5000003007.8300782978.5700683006.7299803006.7299803435540000
2019-09-193010.3601073021.9899903003.1599123006.7900393006.7900393251290000
2019-09-203008.4199223016.3701172984.6799322992.0700682992.0700686094740000
2019-09-232983.5000002999.1499022982.2299802991.7800292991.7800293186590000
\n", "
" ], "text/plain": [ " Open High Low Close Adj Close \\\n", "Date \n", "2019-09-17 2995.669922 3006.209961 2993.729980 3005.699951 3005.699951 \n", "2019-09-18 3001.500000 3007.830078 2978.570068 3006.729980 3006.729980 \n", "2019-09-19 3010.360107 3021.989990 3003.159912 3006.790039 3006.790039 \n", "2019-09-20 3008.419922 3016.370117 2984.679932 2992.070068 2992.070068 \n", "2019-09-23 2983.500000 2999.149902 2982.229980 2991.780029 2991.780029 \n", "\n", " Volume \n", "Date \n", "2019-09-17 3671840000 \n", "2019-09-18 3435540000 \n", "2019-09-19 3251290000 \n", "2019-09-20 6094740000 \n", "2019-09-23 3186590000 " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "sp500 = pd.read_csv(\"data/GSPC.csv\", parse_dates=True, index_col=\"Date\")\n", "hsi = pd.read_csv(\"data/HSI.csv\", parse_dates=True, index_col=\"Date\")\n", "\n", "weekly_sp500 = sp500.resample(\"W-TUE\").last()\n", "weekly_hsi = hsi.resample(\"W-TUE\").last()\n", "\n", "monthly_sp500 = sp500.resample(\"M\").last()\n", "monthly_hsi = hsi.resample(\"M\").last()\n", "\n", "h5file = pd.HDFStore(\"data/equity-indices.h5\", mode=\"w\")\n", "h5file.append(\"sp500\", sp500)\n", "h5file.append(\"weekly_sp500\", weekly_sp500)\n", "h5file.append(\"monthly_sp500\", monthly_sp500)\n", "h5file.append(\"hsi\", sp500)\n", "h5file.append(\"weekly_hsi\", weekly_hsi)\n", "h5file.append(\"monthly_hsi\", monthly_hsi)\n", "h5file.close()\n", "\n", "sp500.tail()" ] }, { "cell_type": "code", "execution_count": 2, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseAdj CloseVolume
Date
2019-08-272893.1398932898.7900392860.5900882869.1599122869.1599123533630000
2019-09-032909.0100102914.3898932891.8500982906.2700202906.2700203426790000
2019-09-102971.0100102979.3898932957.0100102979.3898932979.3898934390770000
2019-09-172995.6699223006.2099612993.7299803005.6999513005.6999513671840000
2019-09-242983.5000002999.1499022982.2299802991.7800292991.7800293186590000
\n", "
" ], "text/plain": [ " Open High Low Close Adj Close \\\n", "Date \n", "2019-08-27 2893.139893 2898.790039 2860.590088 2869.159912 2869.159912 \n", "2019-09-03 2909.010010 2914.389893 2891.850098 2906.270020 2906.270020 \n", "2019-09-10 2971.010010 2979.389893 2957.010010 2979.389893 2979.389893 \n", "2019-09-17 2995.669922 3006.209961 2993.729980 3005.699951 3005.699951 \n", "2019-09-24 2983.500000 2999.149902 2982.229980 2991.780029 2991.780029 \n", "\n", " Volume \n", "Date \n", "2019-08-27 3533630000 \n", "2019-09-03 3426790000 \n", "2019-09-10 4390770000 \n", "2019-09-17 3671840000 \n", "2019-09-24 3186590000 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weekly_sp500.tail()" ] }, { "cell_type": "code", "execution_count": 3, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseAdj CloseVolume
Date
2019-05-312766.1499022768.9799802750.5200202752.0600592752.0600593981020000
2019-06-302932.9399412943.9799802929.0500492941.7600102941.7600105420700000
2019-07-313016.2199713017.3999022958.0800782980.3798832980.3798834623430000
2019-08-312937.0900882940.4299322913.3200682926.4599612926.4599613008450000
2019-09-302983.5000002999.1499022982.2299802991.7800292991.7800293186590000
\n", "
" ], "text/plain": [ " Open High Low Close Adj Close \\\n", "Date \n", "2019-05-31 2766.149902 2768.979980 2750.520020 2752.060059 2752.060059 \n", "2019-06-30 2932.939941 2943.979980 2929.050049 2941.760010 2941.760010 \n", "2019-07-31 3016.219971 3017.399902 2958.080078 2980.379883 2980.379883 \n", "2019-08-31 2937.090088 2940.429932 2913.320068 2926.459961 2926.459961 \n", "2019-09-30 2983.500000 2999.149902 2982.229980 2991.780029 2991.780029 \n", "\n", " Volume \n", "Date \n", "2019-05-31 3981020000 \n", "2019-06-30 5420700000 \n", "2019-07-31 4623430000 \n", "2019-08-31 3008450000 \n", "2019-09-30 3186590000 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "monthly_sp500.tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2\n", "\n", "Write a function that will correctly aggregate to weekly or monthly respecting the\n", "aggregation rules\n", "\n", "* High: `max`\n", "* Low: `min`\n", "* Volume: `sum`\n", "\n", "The signature should be:\n", "\n", "```python\n", "def yahoo_agg(data, freq):\n", " \n", " return resampled_data\n", "```\n" ] }, { "cell_type": "code", "execution_count": 4, "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", "
HighLowVolume
Date
2019-05-312768.9799802750.5200203981020000
2019-06-302943.9799802929.0500495420700000
2019-07-313017.3999022958.0800784623430000
2019-08-312940.4299322913.3200683008450000
2019-09-302999.1499022982.2299803186590000
\n", "
" ], "text/plain": [ " High Low Volume\n", "Date \n", "2019-05-31 2768.979980 2750.520020 3981020000\n", "2019-06-30 2943.979980 2929.050049 5420700000\n", "2019-07-31 3017.399902 2958.080078 4623430000\n", "2019-08-31 2940.429932 2913.320068 3008450000\n", "2019-09-30 2999.149902 2982.229980 3186590000" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def yahoo_agg(data, freq):\n", " resampler = data.resample(freq)\n", "\n", " high = resampler.High.max()\n", " low = resampler.Low.min()\n", " vol = resampler.Volume.sum()\n", " # Start with last for all columns\n", " resampled_data = resampler.last()\n", " # Insert columns that use a different rule\n", " resampled_data[\"High\"] = high\n", " resampled_data[\"Low\"] = low\n", " resampled_data[\"Volume\"] = vol\n", "\n", " return resampled_data\n", "\n", "\n", "better_monthly_sp500 = yahoo_agg(sp500, \"M\")\n", "\n", "monthly_sp500[[\"High\", \"Low\", \"Volume\"]].tail()" ] }, { "cell_type": "code", "execution_count": 5, "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", "
HighLowVolume
Date
2019-05-312954.1298832750.52002076860120000
2019-06-302964.1499022728.81005970881390000
2019-07-313027.9799802952.21997170349470000
2019-08-313013.5900882822.12011779599440000
2019-09-303021.9899902891.85009857236800000
\n", "
" ], "text/plain": [ " High Low Volume\n", "Date \n", "2019-05-31 2954.129883 2750.520020 76860120000\n", "2019-06-30 2964.149902 2728.810059 70881390000\n", "2019-07-31 3027.979980 2952.219971 70349470000\n", "2019-08-31 3013.590088 2822.120117 79599440000\n", "2019-09-30 3021.989990 2891.850098 57236800000" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "better_monthly_sp500[[\"High\", \"Low\", \"Volume\"]].tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3\n", "\n", "1. Import the Fama-French benchmark portfolios as well as the 25 sorted portfolios at both the\n", " monthly and daily horizon from [Ken French\"s Data Library](http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html).\n", " **Note** It is much easier to clean to data file before importing than to find the precise\n", " command that will load the unmodified data.\n", "2. Import daily FX rate data for USD against AUD, Euro, JPY and GBP from the [Federal Reserve Economic Database (FRED)](http://research.stlouisfed.org/fred2/categories/94). Use Excel (xls) rather than csv files.\n", "3. Save the data to the HDF files \"fama-french.h5\" and \"fx.h5\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " DEXJPUS DEXUSAL DEXUSEU DEXUSUK\n", "observation_date \n", "2019-09-16 107.94 0.6865 1.1006 1.2423\n", "2019-09-17 108.12 0.6858 1.1063 1.2493\n", "2019-09-18 108.17 0.6844 1.1061 1.2478\n", "2019-09-19 108.00 0.6795 1.1046 1.2481\n", "2019-09-20 107.95 0.6770 1.1004 1.2489\n" ] } ], "source": [ "yen_dollar = pd.read_excel(\n", " \"data/DEXJPUS.xls\", index_col=\"observation_date\", skiprows=10\n", ")\n", "dollar_aud = pd.read_excel(\n", " \"data/DEXUSAL.xls\", index_col=\"observation_date\", skiprows=10\n", ")\n", "dollar_euro = pd.read_excel(\n", " \"data/DEXUSEU.xls\", index_col=\"observation_date\", skiprows=10\n", ")\n", "dollar_pound = pd.read_excel(\n", " \"data/DEXUSUK.xls\", index_col=\"observation_date\", skiprows=10\n", ")\n", "\n", "fx = pd.concat([yen_dollar, dollar_aud, dollar_euro, dollar_pound], axis=1)\n", "print(fx.tail())\n", "fx.to_hdf(\"data/fx.h5\", \"fx\")" ] }, { "cell_type": "code", "execution_count": 7, "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", " \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", " \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", "
Mkt-RFSMBHMLRFSMALL LoBMME1 BM2ME1 BM3ME1 BM4SMALL HiBMME2 BM1...ME4 BM1ME4 BM2ME4 BM3ME4 BM4ME4 BM5BIG LoBMME5 BM2ME5 BM3ME5 BM4BIG HiBM
2019-03-011.10-3.13-4.070.190.8891-0.4743-2.5100-4.9485-3.4235-0.2319...1.82371.0998-0.9002-0.5395-2.87023.33441.9283-0.1602-0.5929-3.0620
2019-04-013.96-1.681.930.210.60681.65182.18403.49122.08652.8361...3.14104.20464.35015.07624.34194.60392.98073.64295.13419.3863
2019-05-01-6.94-1.20-2.390.21-5.5586-5.3126-7.7829-8.8025-7.9767-8.9190...-5.7334-7.1667-7.7153-9.1637-7.9580-6.9663-5.7641-5.0654-8.1314-8.7586
2019-06-016.930.33-1.080.187.63536.23984.76776.71874.70088.9242...7.28759.40439.51037.51156.92467.74245.88725.96406.66707.9347
2019-07-011.19-2.060.120.19-3.8504-1.1600-0.8926-1.6496-2.5149-3.1247...1.67681.04101.9332-0.66250.99401.65651.91461.50960.98602.1159
\n", "

5 rows × 29 columns

\n", "
" ], "text/plain": [ " Mkt-RF SMB HML RF SMALL LoBM ME1 BM2 ME1 BM3 ME1 BM4 \\\n", "2019-03-01 1.10 -3.13 -4.07 0.19 0.8891 -0.4743 -2.5100 -4.9485 \n", "2019-04-01 3.96 -1.68 1.93 0.21 0.6068 1.6518 2.1840 3.4912 \n", "2019-05-01 -6.94 -1.20 -2.39 0.21 -5.5586 -5.3126 -7.7829 -8.8025 \n", "2019-06-01 6.93 0.33 -1.08 0.18 7.6353 6.2398 4.7677 6.7187 \n", "2019-07-01 1.19 -2.06 0.12 0.19 -3.8504 -1.1600 -0.8926 -1.6496 \n", "\n", " SMALL HiBM ME2 BM1 ... ME4 BM1 ME4 BM2 ME4 BM3 ME4 BM4 \\\n", "2019-03-01 -3.4235 -0.2319 ... 1.8237 1.0998 -0.9002 -0.5395 \n", "2019-04-01 2.0865 2.8361 ... 3.1410 4.2046 4.3501 5.0762 \n", "2019-05-01 -7.9767 -8.9190 ... -5.7334 -7.1667 -7.7153 -9.1637 \n", "2019-06-01 4.7008 8.9242 ... 7.2875 9.4043 9.5103 7.5115 \n", "2019-07-01 -2.5149 -3.1247 ... 1.6768 1.0410 1.9332 -0.6625 \n", "\n", " ME4 BM5 BIG LoBM ME5 BM2 ME5 BM3 ME5 BM4 BIG HiBM \n", "2019-03-01 -2.8702 3.3344 1.9283 -0.1602 -0.5929 -3.0620 \n", "2019-04-01 4.3419 4.6039 2.9807 3.6429 5.1341 9.3863 \n", "2019-05-01 -7.9580 -6.9663 -5.7641 -5.0654 -8.1314 -8.7586 \n", "2019-06-01 6.9246 7.7424 5.8872 5.9640 6.6670 7.9347 \n", "2019-07-01 0.9940 1.6565 1.9146 1.5096 0.9860 2.1159 \n", "\n", "[5 rows x 29 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# These files have all been cleaned to have only the data and headers\n", "ff_5x5 = pd.read_csv(\"data/25_Portfolios_5x5.CSV\", index_col=0)\n", "ff_factors = pd.read_csv(\"data/F-F_Research_Data_Factors.CSV\", index_col=0)\n", "ff = pd.concat([ff_factors, ff_5x5], axis=1)\n", "\n", "dates = []\n", "for value in ff.index:\n", " # Values are YYYYMM\n", " year = value // 100\n", " month = value % 100\n", " dates.append(pd.Timestamp(year=year, month=month, day=1))\n", "ff.index = dates\n", "ff.tail()" ] }, { "cell_type": "code", "execution_count": 8, "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", " \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", " \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", "
Mkt-RFSMBHMLRFSMALL LoBMME1 BM2ME1 BM3ME1 BM4SMALL HiBMME2 BM1...ME4 BM1ME4 BM2ME4 BM3ME4 BM4ME4 BM5BIG LoBMME5 BM2ME5 BM3ME5 BM4BIG HiBM
2019-03-311.10-3.13-4.070.190.8891-0.4743-2.5100-4.9485-3.4235-0.2319...1.82371.0998-0.9002-0.5395-2.87023.33441.9283-0.1602-0.5929-3.0620
2019-04-303.96-1.681.930.210.60681.65182.18403.49122.08652.8361...3.14104.20464.35015.07624.34194.60392.98073.64295.13419.3863
2019-05-31-6.94-1.20-2.390.21-5.5586-5.3126-7.7829-8.8025-7.9767-8.9190...-5.7334-7.1667-7.7153-9.1637-7.9580-6.9663-5.7641-5.0654-8.1314-8.7586
2019-06-306.930.33-1.080.187.63536.23984.76776.71874.70088.9242...7.28759.40439.51037.51156.92467.74245.88725.96406.66707.9347
2019-07-311.19-2.060.120.19-3.8504-1.1600-0.8926-1.6496-2.5149-3.1247...1.67681.04101.9332-0.66250.99401.65651.91461.50960.98602.1159
\n", "

5 rows × 29 columns

\n", "
" ], "text/plain": [ " Mkt-RF SMB HML RF SMALL LoBM ME1 BM2 ME1 BM3 ME1 BM4 \\\n", "2019-03-31 1.10 -3.13 -4.07 0.19 0.8891 -0.4743 -2.5100 -4.9485 \n", "2019-04-30 3.96 -1.68 1.93 0.21 0.6068 1.6518 2.1840 3.4912 \n", "2019-05-31 -6.94 -1.20 -2.39 0.21 -5.5586 -5.3126 -7.7829 -8.8025 \n", "2019-06-30 6.93 0.33 -1.08 0.18 7.6353 6.2398 4.7677 6.7187 \n", "2019-07-31 1.19 -2.06 0.12 0.19 -3.8504 -1.1600 -0.8926 -1.6496 \n", "\n", " SMALL HiBM ME2 BM1 ... ME4 BM1 ME4 BM2 ME4 BM3 ME4 BM4 \\\n", "2019-03-31 -3.4235 -0.2319 ... 1.8237 1.0998 -0.9002 -0.5395 \n", "2019-04-30 2.0865 2.8361 ... 3.1410 4.2046 4.3501 5.0762 \n", "2019-05-31 -7.9767 -8.9190 ... -5.7334 -7.1667 -7.7153 -9.1637 \n", "2019-06-30 4.7008 8.9242 ... 7.2875 9.4043 9.5103 7.5115 \n", "2019-07-31 -2.5149 -3.1247 ... 1.6768 1.0410 1.9332 -0.6625 \n", "\n", " ME4 BM5 BIG LoBM ME5 BM2 ME5 BM3 ME5 BM4 BIG HiBM \n", "2019-03-31 -2.8702 3.3344 1.9283 -0.1602 -0.5929 -3.0620 \n", "2019-04-30 4.3419 4.6039 2.9807 3.6429 5.1341 9.3863 \n", "2019-05-31 -7.9580 -6.9663 -5.7641 -5.0654 -8.1314 -8.7586 \n", "2019-06-30 6.9246 7.7424 5.8872 5.9640 6.6670 7.9347 \n", "2019-07-31 0.9940 1.6565 1.9146 1.5096 0.9860 2.1159 \n", "\n", "[5 rows x 29 columns]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This is a \"trick\" to get the index to have the last day in the month.\n", "ff = ff.resample(\"M\").last()\n", "\n", "ff.to_hdf(\"data/ff.h5\", \"ff\")\n", "\n", "ff.tail()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['1926-07-31', '1926-08-31', '1926-09-30', '1926-10-31',\n", " '1926-11-30', '1926-12-31', '1927-01-31', '1927-02-28',\n", " '1927-03-31', '1927-04-30',\n", " ...\n", " '2018-10-31', '2018-11-30', '2018-12-31', '2019-01-31',\n", " '2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31',\n", " '2019-06-30', '2019-07-31'],\n", " dtype='datetime64[ns]', length=1117, freq='M')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ff.index" ] }, { "cell_type": "code", "execution_count": 10, "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", " \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", " \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", "
Mkt-RFSMBHMLRFSMALL LoBMME1 BM2ME1 BM3ME1 BM4SMALL HiBMME2 BM1...ME4 BM1ME4 BM2ME4 BM3ME4 BM4ME4 BM5BIG LoBMME5 BM2ME5 BM3ME5 BM4BIG HiBM
2019-07-25-0.63-0.69-0.210.009-1.89-1.59-1.02-1.11-1.59-1.90...-0.95-0.72-0.74-0.34-1.23-0.62-0.27-0.83-0.48-1.68
2019-07-260.820.39-0.210.0091.531.481.291.180.681.37...0.840.650.960.170.270.531.540.610.621.37
2019-07-29-0.32-0.32-0.470.009-0.340.16-0.89-0.62-0.99-0.82...-0.93-0.36-0.24-0.86-0.85-0.05-0.34-0.32-0.33-0.91
2019-07-30-0.171.310.860.0091.041.490.841.212.130.86...-0.22-0.180.410.951.25-0.27-0.70-0.100.16-0.48
2019-07-31-1.090.130.560.009-0.90-1.76-0.43-0.73-0.43-1.44...-0.98-1.30-0.68-0.65-1.32-1.24-1.23-1.24-0.65-0.64
\n", "

5 rows × 29 columns

\n", "
" ], "text/plain": [ " Mkt-RF SMB HML RF SMALL LoBM ME1 BM2 ME1 BM3 ME1 BM4 \\\n", "2019-07-25 -0.63 -0.69 -0.21 0.009 -1.89 -1.59 -1.02 -1.11 \n", "2019-07-26 0.82 0.39 -0.21 0.009 1.53 1.48 1.29 1.18 \n", "2019-07-29 -0.32 -0.32 -0.47 0.009 -0.34 0.16 -0.89 -0.62 \n", "2019-07-30 -0.17 1.31 0.86 0.009 1.04 1.49 0.84 1.21 \n", "2019-07-31 -1.09 0.13 0.56 0.009 -0.90 -1.76 -0.43 -0.73 \n", "\n", " SMALL HiBM ME2 BM1 ... ME4 BM1 ME4 BM2 ME4 BM3 ME4 BM4 \\\n", "2019-07-25 -1.59 -1.90 ... -0.95 -0.72 -0.74 -0.34 \n", "2019-07-26 0.68 1.37 ... 0.84 0.65 0.96 0.17 \n", "2019-07-29 -0.99 -0.82 ... -0.93 -0.36 -0.24 -0.86 \n", "2019-07-30 2.13 0.86 ... -0.22 -0.18 0.41 0.95 \n", "2019-07-31 -0.43 -1.44 ... -0.98 -1.30 -0.68 -0.65 \n", "\n", " ME4 BM5 BIG LoBM ME5 BM2 ME5 BM3 ME5 BM4 BIG HiBM \n", "2019-07-25 -1.23 -0.62 -0.27 -0.83 -0.48 -1.68 \n", "2019-07-26 0.27 0.53 1.54 0.61 0.62 1.37 \n", "2019-07-29 -0.85 -0.05 -0.34 -0.32 -0.33 -0.91 \n", "2019-07-30 1.25 -0.27 -0.70 -0.10 0.16 -0.48 \n", "2019-07-31 -1.32 -1.24 -1.23 -1.24 -0.65 -0.64 \n", "\n", "[5 rows x 29 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# These files have all been cleaned to have only the data and headers\n", "ff_5x5_daily = pd.read_csv(\"data/25_Portfolios_5x5_daily.CSV\", index_col=0)\n", "ff_factors_daily = pd.read_csv(\"data/F-F_Research_Data_Factors_daily.CSV\", index_col=0)\n", "ff_daily = pd.concat([ff_factors_daily, ff_5x5_daily], axis=1)\n", "\n", "\n", "dates = []\n", "for value in ff_daily.index:\n", " # Values are YYYYMMDD\n", " year = value // 10000\n", " month = (value // 100) % 100\n", " day = value % 100\n", " dates.append(pd.Timestamp(year=year, month=month, day=day))\n", "ff_daily.index = dates\n", "ff_daily.to_hdf(\"data/ff_daily.h5\", \"ff_daily\")\n", "\n", "ff_daily.tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3 (Alternative method)\n", "\n", "1. Install and use `pandas-datareader` to repeat the previous exercise.\n", "\n", "#### Preliminary Step\n", "\n", "You must first install the module using \n", "\n", "```\n", "pip install pandas-datareader\n", "``` \n", "\n", "from the command line. Then you can run this code. **Note**: Running this code requires access\n", "to the internet." ] }, { "cell_type": "code", "execution_count": 11, "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", " \n", "
DEXJPUSDEXUSALDEXUSEUDEXUSUK
DATE
2024-02-19NaNNaNNaNNaN
2024-02-20149.870.65601.08141.2645
2024-02-21150.290.65461.08181.2624
2024-02-22150.620.65471.08151.2649
2024-02-23150.360.65661.08281.2678
\n", "
" ], "text/plain": [ " DEXJPUS DEXUSAL DEXUSEU DEXUSUK\n", "DATE \n", "2024-02-19 NaN NaN NaN NaN\n", "2024-02-20 149.87 0.6560 1.0814 1.2645\n", "2024-02-21 150.29 0.6546 1.0818 1.2624\n", "2024-02-22 150.62 0.6547 1.0815 1.2649\n", "2024-02-23 150.36 0.6566 1.0828 1.2678" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas_datareader as pdr\n", "\n", "# Conservative start date to get all data\n", "yen_dollar = pdr.get_data_fred(\"DEXJPUS\", start=\"1950\")\n", "dollar_aud = pdr.get_data_fred(\"DEXUSAL\", start=\"1950\")\n", "dollar_euro = pdr.get_data_fred(\"DEXUSEU\", start=\"1950\")\n", "dollar_pound = pdr.get_data_fred(\"DEXUSUK\", start=\"1950\")\n", "fx = pd.concat([yen_dollar, dollar_aud, dollar_euro, dollar_pound], axis=1)\n", "fx.to_hdf(\"data/fx-pdr.h5\", \"fx\")\n", "fx.tail()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:1: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_factors = pdr.get_data_famafrench(\"F-F_Research_Data_Factors\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:1: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_factors = pdr.get_data_famafrench(\"F-F_Research_Data_Factors\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "C:\\Users\\kevin\\AppData\\Local\\Temp\\ipykernel_6172\\127267269.py:2: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n", " ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n" ] }, { "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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Mkt-RFSMBHMLRFSMALL LoBMME1 BM2ME1 BM3ME1 BM4SMALL HiBMME2 BM1...ME4 BM1ME4 BM2ME4 BM3ME4 BM4ME4 BM5BIG LoBMME5 BM2ME5 BM3ME5 BM4BIG HiBM
Date
2023-08-2.39-3.16-1.060.45-12.2376-7.6627-10.5103-5.6405-7.3431-6.9345...-2.4000-2.3534-2.8719-3.8686-2.1140-0.8338-0.9250-2.6918-2.9058-7.3853
2023-09-5.24-2.511.520.43-8.2275-7.6201-6.5856-5.8185-6.0216-8.8904...-5.6917-4.6306-6.5792-5.0335-3.4990-5.5603-4.3422-1.4851-2.1397-2.2370
2023-10-3.19-3.870.190.47-10.2240-8.9443-7.9196-6.3774-7.6783-10.2040...-5.9518-5.8296-7.1202-5.7789-6.2824-0.9824-4.9262-2.5643-2.6864-4.0377
2023-118.84-0.021.640.445.78618.073710.73198.50407.094510.9883...9.87438.97909.52798.72387.84769.89467.01767.09358.661415.2510
2023-124.856.354.940.4315.321616.038314.863516.460514.918212.0839...8.65687.76529.18219.009411.72103.72894.71295.08414.659211.6076
\n", "

5 rows × 29 columns

\n", "
" ], "text/plain": [ " Mkt-RF SMB HML RF SMALL LoBM ME1 BM2 ME1 BM3 ME1 BM4 \\\n", "Date \n", "2023-08 -2.39 -3.16 -1.06 0.45 -12.2376 -7.6627 -10.5103 -5.6405 \n", "2023-09 -5.24 -2.51 1.52 0.43 -8.2275 -7.6201 -6.5856 -5.8185 \n", "2023-10 -3.19 -3.87 0.19 0.47 -10.2240 -8.9443 -7.9196 -6.3774 \n", "2023-11 8.84 -0.02 1.64 0.44 5.7861 8.0737 10.7319 8.5040 \n", "2023-12 4.85 6.35 4.94 0.43 15.3216 16.0383 14.8635 16.4605 \n", "\n", " SMALL HiBM ME2 BM1 ... ME4 BM1 ME4 BM2 ME4 BM3 ME4 BM4 \\\n", "Date ... \n", "2023-08 -7.3431 -6.9345 ... -2.4000 -2.3534 -2.8719 -3.8686 \n", "2023-09 -6.0216 -8.8904 ... -5.6917 -4.6306 -6.5792 -5.0335 \n", "2023-10 -7.6783 -10.2040 ... -5.9518 -5.8296 -7.1202 -5.7789 \n", "2023-11 7.0945 10.9883 ... 9.8743 8.9790 9.5279 8.7238 \n", "2023-12 14.9182 12.0839 ... 8.6568 7.7652 9.1821 9.0094 \n", "\n", " ME4 BM5 BIG LoBM ME5 BM2 ME5 BM3 ME5 BM4 BIG HiBM \n", "Date \n", "2023-08 -2.1140 -0.8338 -0.9250 -2.6918 -2.9058 -7.3853 \n", "2023-09 -3.4990 -5.5603 -4.3422 -1.4851 -2.1397 -2.2370 \n", "2023-10 -6.2824 -0.9824 -4.9262 -2.5643 -2.6864 -4.0377 \n", "2023-11 7.8476 9.8946 7.0176 7.0935 8.6614 15.2510 \n", "2023-12 11.7210 3.7289 4.7129 5.0841 4.6592 11.6076 \n", "\n", "[5 rows x 29 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ff_factors = pdr.get_data_famafrench(\"F-F_Research_Data_Factors\", start=\"1920\")\n", "ff_5x5 = pdr.get_data_famafrench(\"25_Portfolios_5x5\", start=\"1920\")\n", "# The function returns all of the tables in each file. We want the first, [0]\n", "ff_factors = ff_factors[0]\n", "ff_5x5 = ff_5x5[0]\n", "ff = pd.concat([ff_factors, ff_5x5], axis=1)\n", "ff.to_hdf(\"data/ff-pdr.h5\", \"ff\")\n", "ff.tail()" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Exercise 4\n", "Download data on 1 year and 10 year US government bond rates from FRED, and \n", "construct the term premium as the different in yields on 10 year and 1 year\n", "bonds. Combine the two yield series and the term premium into a `DataFrame`\n", "and save it as HDF." ] }, { "cell_type": "code", "execution_count": 13, "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", "
TERMGS10GS1
DATE
2023-09-01-1.064.385.44
2023-10-01-0.624.805.42
2023-11-01-0.784.505.28
2023-12-01-0.944.024.96
2024-01-01-0.734.064.79
\n", "
" ], "text/plain": [ " TERM GS10 GS1\n", "DATE \n", "2023-09-01 -1.06 4.38 5.44\n", "2023-10-01 -0.62 4.80 5.42\n", "2023-11-01 -0.78 4.50 5.28\n", "2023-12-01 -0.94 4.02 4.96\n", "2024-01-01 -0.73 4.06 4.79" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# No need to import here since pandas and pandas-datareader previously imported\n", "\n", "# Conservative start date to get all data\n", "gs10 = pdr.get_data_fred(\"GS10\", start=\"1950\")\n", "gs1 = pdr.get_data_fred(\"GS1\", start=\"1950\")\n", "\n", "term = gs10[\"GS10\"] - gs1[\"GS1\"]\n", "term.name = \"TERM\"\n", "combined = pd.DataFrame([term, gs10[\"GS10\"], gs1[\"GS1\"]]).T\n", "combined.tail()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['1953-04-01', '1953-05-01', '1953-06-01', '1953-07-01',\n", " '1953-08-01', '1953-09-01', '1953-10-01', '1953-11-01',\n", " '1953-12-01', '1954-01-01',\n", " ...\n", " '2023-04-01', '2023-05-01', '2023-06-01', '2023-07-01',\n", " '2023-08-01', '2023-09-01', '2023-10-01', '2023-11-01',\n", " '2023-12-01', '2024-01-01'],\n", " dtype='datetime64[ns]', name='DATE', length=850, freq=None)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "combined.index" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['1953-04-01', '1953-05-01', '1953-06-01', '1953-07-01',\n", " '1953-08-01', '1953-09-01', '1953-10-01', '1953-11-01',\n", " '1953-12-01', '1954-01-01',\n", " ...\n", " '2023-04-01', '2023-05-01', '2023-06-01', '2023-07-01',\n", " '2023-08-01', '2023-09-01', '2023-10-01', '2023-11-01',\n", " '2023-12-01', '2024-01-01'],\n", " dtype='datetime64[ns]', name='DATE', length=850, freq='MS')" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Trick to ensure the index has the frequency MS, Month Start\n", "combined = combined.resample(\"MS\").last()\n", "combined.to_hdf(\"data/term-premium.h5\", \"term_premium\")\n", "combined.index" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 4 }