In [1]:
import pandas as pd
from datetime import *
import os
import urllib

Download the .xls file:


In [2]:
# this determine the url where the file can be found
url = 'http://www.federalreserve.gov/econresdata/researchdata/feds200628.xls'

In [3]:
# this opens the url
f = urllib.request.urlopen(url)

In [4]:
# the downloads the url and makes a local file. basename will name the file with the last bit of text before the .xls (so feds200628)
with open(os.path.basename(url), "wb") as local_file:
    local_file.write(f.read())

Open the .xls file and convert to a dataframe:


In [10]:
#simple Pandas to open a file and make a df
xls_file = pd.ExcelFile(os.path.basename(url))

At this point, you'll hit an error becayse this file is basically .xml in disguise. However, if you simply open the file in Excel, do "save as" and keep it exactly the same, inlcuding still in .xls format, you can return to this script and keep running it as normal without any further changes


In [11]:
xls_df = xls_file.parse('Yields', header = 9).reset_index()

In [12]:
xls_df.rename(columns={'index':'Date'}, inplace=True)

Set Date to Datetime format, make it the index:


In [13]:
xls_df.Date = pd.to_datetime(xls_df.Date)

In [14]:
xls_df_indexed = xls_df.set_index('Date')

Limit the dataframe to just the last day of each month:


In [15]:
xls_df_indexed[xls_df_indexed.index.is_month_end == True].head()


Out[15]:
SVENY01 SVENY02 SVENY03 SVENY04 SVENY05 SVENY06 SVENY07 SVENY08 SVENY09 SVENY10 ... SVENF30 SVEN1F01 SVEN1F04 SVEN1F09 BETA0 BETA1 BETA2 BETA3 TAU1 TAU2
Date
2014-07-31 0.1253 0.5487 1.0534 1.4776 1.8078 2.0634 2.2649 2.4277 2.5623 2.6758 ... 3.9435 0.9738 3.1528 3.7316 3.944796 -3.465690 -5.470553 -1.582482 0.954930 3.219709
2014-06-30 0.1279 0.4543 0.8938 1.3028 1.6474 1.9290 2.1584 2.3467 2.5031 2.6346 ... 4.0037 0.7818 3.0478 3.8547 4.004024 -3.643984 -4.872440 -1.580971 1.160778 2.753113
2014-04-30 0.1019 0.4245 0.8904 1.3378 1.7197 2.0318 2.2836 2.4869 2.6525 2.7890 ... 4.0961 0.7481 3.2730 4.0574 4.096318 -3.727558 -5.779416 -0.327856 1.288027 3.192016
2014-03-31 0.1215 0.4393 0.9157 1.3738 1.7640 2.0826 2.3399 2.5483 2.7188 2.8600 ... 4.2487 0.7580 3.3515 4.1738 4.248987 -3.782516 -5.978609 -0.716158 1.239905 2.977916
2014-02-28 0.1220 0.3393 0.7286 1.1484 1.5398 1.8826 2.1744 2.4197 2.6250 2.7970 ... 4.1465 0.5570 3.1283 4.3930 3.924353 -3.537562 -5.940922 1.431239 1.600506 10.196275

5 rows × 99 columns

Or, if you prefer to save the file as a .csv instead:


In [16]:
csv_df = pd.read_csv('/Users/sarahbeckett-hile/Downloads/feds200628(3).csv', skiprows=9) #will need to customize

In [17]:
csv_df.rename(columns={'Unnamed: 0':'Date'}, inplace=True)

In [18]:
csv_df.Date = pd.to_datetime(csv_df.Date)

In [19]:
csv_df_indexed = csv_df.set_index('Date')

In [20]:
csv_df_indexed[csv_df_indexed.index.is_month_end == True].head()


Out[20]:
SVENY01 SVENY02 SVENY03 SVENY04 SVENY05 SVENY06 SVENY07 SVENY08 SVENY09 SVENY10 ... SVENF30 SVEN1F01 SVEN1F04 SVEN1F09 BETA0 BETA1 BETA2 BETA3 TAU1 TAU2
Date
2014-07-31 0.1253 0.5487 1.0534 1.4776 1.8078 2.0634 2.2649 2.4277 2.5623 2.6758 ... 3.9435 0.9738 3.1528 3.7316 3.944796 -3.465690 -5.470553 -1.582482 0.954930 3.219709
2014-06-30 0.1279 0.4543 0.8938 1.3028 1.6474 1.9290 2.1584 2.3467 2.5031 2.6346 ... 4.0037 0.7818 3.0478 3.8547 4.004024 -3.643984 -4.872440 -1.580971 1.160778 2.753113
2014-04-30 0.1019 0.4245 0.8904 1.3378 1.7197 2.0318 2.2836 2.4869 2.6525 2.7890 ... 4.0961 0.7481 3.2730 4.0574 4.096318 -3.727558 -5.779416 -0.327856 1.288027 3.192016
2014-03-31 0.1215 0.4393 0.9157 1.3738 1.7640 2.0826 2.3399 2.5483 2.7188 2.8600 ... 4.2487 0.7580 3.3515 4.1738 4.248987 -3.782516 -5.978609 -0.716158 1.239905 2.977916
2014-02-28 0.1220 0.3393 0.7286 1.1484 1.5398 1.8826 2.1744 2.4197 2.6250 2.7970 ... 4.1465 0.5570 3.1283 4.3930 3.924353 -3.537562 -5.940922 1.431239 1.600506 10.196275

5 rows × 99 columns


In [ ]: