{ "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(\"ME\").last()\n", "monthly_hsi = hsi.resample(\"ME\").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, \"ME\")\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\", key=\"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(\"ME\").last()\n", "\n", "ff.to_hdf(\"data/ff.h5\", key=\"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='ME')" ] }, "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\", key=\"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
2025-08-18147.760.64941.16671.3521
2025-08-19147.720.64591.16601.3496
2025-08-20147.190.64281.16491.3450
2025-08-21148.350.64211.16051.3417
2025-08-22146.820.64851.17131.3528
\n", "
" ], "text/plain": [ " DEXJPUS DEXUSAL DEXUSEU DEXUSUK\n", "DATE \n", "2025-08-18 147.76 0.6494 1.1667 1.3521\n", "2025-08-19 147.72 0.6459 1.1660 1.3496\n", "2025-08-20 147.19 0.6428 1.1649 1.3450\n", "2025-08-21 148.35 0.6421 1.1605 1.3417\n", "2025-08-22 146.82 0.6485 1.1713 1.3528" ] }, "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\", key=\"fx\")\n", "fx.tail()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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\\python\\AppData\\Local\\Temp\\ipykernel_50256\\1223250173.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
2025-02-2.44-5.794.910.33-7.0665-5.9742-6.5071-6.0932-2.3476-8.2831...-4.2825-6.7574-2.2267-2.8109-1.7042-3.1108-1.88392.54592.86621.5498
2025-03-6.39-2.762.900.34-13.9313-12.1005-9.4072-9.7306-6.6402-8.3570...-6.6421-5.8203-3.2662-6.4330-1.7756-7.6632-4.9980-0.1605-2.1792-6.3793
2025-04-0.84-0.59-3.400.350.80671.69030.8943-0.5912-2.52500.5968...-0.8765-1.2698-2.0145-3.9275-7.26681.4105-3.0129-7.3866-1.3472-2.7941
2025-056.060.70-2.880.3810.84485.76235.34314.24795.71714.4605...6.25765.02213.53538.11746.58257.80776.12951.84062.61556.5683
2025-064.860.83-1.600.3413.76765.87778.64034.68915.53476.2285...2.03514.31922.41757.38155.80245.52786.24504.74043.64247.0108
\n", "

5 rows × 29 columns

\n", "
" ], "text/plain": [ " Mkt-RF SMB HML RF SMALL LoBM ME1 BM2 ME1 BM3 ME1 BM4 \\\n", "Date \n", "2025-02 -2.44 -5.79 4.91 0.33 -7.0665 -5.9742 -6.5071 -6.0932 \n", "2025-03 -6.39 -2.76 2.90 0.34 -13.9313 -12.1005 -9.4072 -9.7306 \n", "2025-04 -0.84 -0.59 -3.40 0.35 0.8067 1.6903 0.8943 -0.5912 \n", "2025-05 6.06 0.70 -2.88 0.38 10.8448 5.7623 5.3431 4.2479 \n", "2025-06 4.86 0.83 -1.60 0.34 13.7676 5.8777 8.6403 4.6891 \n", "\n", " SMALL HiBM ME2 BM1 ... ME4 BM1 ME4 BM2 ME4 BM3 ME4 BM4 \\\n", "Date ... \n", "2025-02 -2.3476 -8.2831 ... -4.2825 -6.7574 -2.2267 -2.8109 \n", "2025-03 -6.6402 -8.3570 ... -6.6421 -5.8203 -3.2662 -6.4330 \n", "2025-04 -2.5250 0.5968 ... -0.8765 -1.2698 -2.0145 -3.9275 \n", "2025-05 5.7171 4.4605 ... 6.2576 5.0221 3.5353 8.1174 \n", "2025-06 5.5347 6.2285 ... 2.0351 4.3192 2.4175 7.3815 \n", "\n", " ME4 BM5 BIG LoBM ME5 BM2 ME5 BM3 ME5 BM4 BIG HiBM \n", "Date \n", "2025-02 -1.7042 -3.1108 -1.8839 2.5459 2.8662 1.5498 \n", "2025-03 -1.7756 -7.6632 -4.9980 -0.1605 -2.1792 -6.3793 \n", "2025-04 -7.2668 1.4105 -3.0129 -7.3866 -1.3472 -2.7941 \n", "2025-05 6.5825 7.8077 6.1295 1.8406 2.6155 6.5683 \n", "2025-06 5.8024 5.5278 6.2450 4.7404 3.6424 7.0108 \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\", key=\"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
2025-03-010.224.284.06
2025-04-010.334.283.95
2025-05-010.334.424.09
2025-06-010.324.384.06
2025-07-010.314.394.08
\n", "
" ], "text/plain": [ " TERM GS10 GS1\n", "DATE \n", "2025-03-01 0.22 4.28 4.06\n", "2025-04-01 0.33 4.28 3.95\n", "2025-05-01 0.33 4.42 4.09\n", "2025-06-01 0.32 4.38 4.06\n", "2025-07-01 0.31 4.39 4.08" ] }, "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", " '2024-10-01', '2024-11-01', '2024-12-01', '2025-01-01',\n", " '2025-02-01', '2025-03-01', '2025-04-01', '2025-05-01',\n", " '2025-06-01', '2025-07-01'],\n", " dtype='datetime64[ns]', name='DATE', length=868, 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", " '2024-10-01', '2024-11-01', '2024-12-01', '2025-01-01',\n", " '2025-02-01', '2025-03-01', '2025-04-01', '2025-05-01',\n", " '2025-06-01', '2025-07-01'],\n", " dtype='datetime64[ns]', name='DATE', length=868, 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\", key=\"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.13.5" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 4 }