Download Data From FRED

The FRED website hosts loads of economic data that is freely available. You can get a lot of information from using the site interactively but we’ll also show a few examples here about how to download series using Microsoft Excel and how to pull series directly into python data structures.

To use FRED from Excel it is necessary to download the add-in that they provide. When installed you should see the following in your Excel ribbon

This image has an empty alt attribute; its file name is fred-excel-ribbon.jpg

An example of this using this add-in is the spreadsheet FRED in Excel.xlsx which shows an example of downloading US Treasury yield series either individually or as a set.

This add-in does not provide a full api within Excel but if you don’t know how to program it provides a way to pull arbitrary FRED series into a spreadsheet for further analysis.

FRED From Python

The developer api page lists a number of python modules that are wrappers around the FRED api. Unfortunately I did not find any of them to be available through Anaconda which is the distribution I prefer. All the listed python wrappers have source code available on GitHub but instead of trying to update the code for my python distribution I thought I’d just set up some python code to download series data as an example. In the following code I download a price series using first the JSON response and then the XML response. The data is then used to create a pandas Series.

import json
import xml.etree.ElementTree as ET
import requests
import datetime
import numpy as np
import pandas as pd

series_code = 'DGS10'
api_key= '51151e9fcbe76a6f1803926721bbab40' # Please request your own key. This is not guaranteed to work forever
# https://research.stlouisfed.org/useraccount/apikeys

start_date = datetime.datetime(2019, 1, 1)
end_date = datetime.datetime.now(2019, 1, 19)
series_request_pre = 'https://api.stlouisfed.org/fred/series/observations?series_id='

# Use JSON to get results
file_type = 'json'
series_request_url = series_request_pre + series_code  \
                        + '&api_key=' + api_key        \
                        + '&file_type=' + file_type    \
                        + '&observation_start=' + start_date.strftime("%Y-%m-%d") \
                        + '&observation_end=' + end_date.strftime("%Y-%m-%d")

response = requests.get(series_request_url)
json_content = response.json()

series = {}
for jc in json_content['observations'] :
    try :
        d = datetime.datetime.strptime(jc['date'],'%Y-%m-%d')
        val = float(jc['value'])
        series[d] = val
    except ValueError :
        continue

pandas_series = pd.Series(series, name = series_code)
print("Series from JSON")
print(pandas_series)

#use XML to get results
file_type = 'xml'
series_request_url = series_request_pre + series_code  \
                        + '&api_key=' + api_key        \
                        + '&file_type=' + file_type    \
                        + '&observation_start=' + start_date.strftime("%Y-%m-%d") \
                        + '&observation_end=' + end_date.strftime("%Y-%m-%d")

response = requests.get(series_request_url)
xml_content = response.content

xml_tree = ET.fromstring(xml_content)
series = {}
for child in xml_tree :
    try :
        d = datetime.datetime.strptime(child.attrib['date'],'%Y-%m-%d')
        val = float(child.attrib['value'])
        series[d] = val
    except ValueError :
        continue

pandas_series = pd.Series(series, name = series_code)
print("\nSeries From XML")
print(pandas_series)

the output should be

Series from JSON
2019-01-02    2.66
2019-01-03    2.56
2019-01-04    2.67
2019-01-07    2.70
2019-01-08    2.73
2019-01-09    2.74
2019-01-10    2.74
2019-01-11    2.71
2019-01-14    2.71
2019-01-15    2.72
2019-01-16    2.73
2019-01-17    2.75
2019-01-18    2.79
Name: DGS10, dtype: float64

Series From XML
2019-01-02    2.66
2019-01-03    2.56
2019-01-04    2.67
2019-01-07    2.70
2019-01-08    2.73
2019-01-09    2.74
2019-01-10    2.74
2019-01-11    2.71
2019-01-14    2.71
2019-01-15    2.72
2019-01-16    2.73
2019-01-17    2.75
2019-01-18    2.79
Name: DGS10, dtype: float64

Have Fun!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s