FRED data
FRED (Federal Reserve Economic Data) is a vast database of economic data provided by the Federal Reserve Bank of St. Louis. It currently contains 237,000 data series and it continues to expand.
I wrote a simple python module called fredapi
that makes it easy to access the FRED data. It returns data in pandas
data structures.
This module also makes it easy to deal with data revisions. Many economic data series contain frequent revisions for various reasons. fredapi
provides several convenient methods for handling data revisions and answering the quesion of what-data-was-known-when.
The system in FRED that contains historic data revisions is also known as ALFRED (ArchivaL Federal Reserve Economic Data.) You can look at my github page for more detailed examples related to ALFRED and data revisions.
Installing
The easiest way to install fredapi is through pip:
pip install fredapi
You can also download or view the code from my github page.
Example usage
Import the fredapi
module. Note that I have set my api key to the environment variable FRED_API_KEY
. You can also pass your key explicitly.
from fredapi import Fred
fred = Fred()
Import pandas
and several display and plotting options
import pandas as pd
pd.options.display.max_colwidth = 60
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.core.pylabtools import figsize
figsize(20, 5)
If you already know the series ID you want (say by searching on the FRED website), you can fetch data easily into a pandas Series
s = fred.get_series('SP500', observation_start='2014-09-02', observation_end='2014-09-05')
s.tail()
s = fred.get_series('SP500', observation_start='1/31/2014')
s.tail()
You can also easily fetch the meta data about any series
info = fred.get_series_info('PAYEMS')
info['title']
You can also get a set of series IDs programmatically by release or category IDs. Several sorting options are also available. On the FRED website I know that the release ID 175 contains some personal income data. Let's fetch 5 most popular series in that set.
personal_income_series = fred.search_by_release(175, limit=5, order_by='popularity', sort_order='desc')
personal_income_series['title']
df = {}
df['SF'] = fred.get_series('PCPI06075')
df['NY'] = fred.get_series('PCPI36061')
df['DC'] = fred.get_series('PCPI11001')
df = pd.DataFrame(df)
df.plot()
You can also search by categories. On the FRED website I see that category 101
contains data about Consumer Credit.
df = fred.search_by_category(101, limit=10, order_by='popularity', sort_order='desc')
df['title']
As a example let's fetch the personal income data. Release 151
looks quite intersting
df = fred.search_by_release(151)
df['title'].head(10)
I noticed that the data is mostly organized by state, except for a few that are by BEA region. We can use pandas
to easily select the seires we want
state_df = df[~df['title'].str.startswith('Per Capita Personal Income in the')]
len(state_df)
state_df.id.str[:2]
looks good, we got the data series for all 50 states here
income_by_state = {}
for series_id in state_df.index:
income_by_state[series_id[:2]] = fred.get_series(series_id)
income_by_state = pd.DataFrame(income_by_state)
income_by_state.ix[-1].plot(kind='bar')
plt.title('Per Capita Personal Income by State')