Random Points

Python API for FRED

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.

In [1]:
from fredapi import Fred
fred = Fred()

Import pandas and several display and plotting options

In [2]:
import pandas as pd
pd.options.display.max_colwidth = 60
In [3]:
%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

In [4]:
s = fred.get_series('SP500', observation_start='2014年09月02日', observation_end='2014年09月05日')
s.tail()
Out[4]:
2014年09月02日 2002.28
2014年09月03日 2000.72
2014年09月04日 1997.65
2014年09月05日 2007.71
dtype: float64
In [5]:
s = fred.get_series('SP500', observation_start='1/31/2014')
s.tail()
Out[5]:
2014年11月19日 2048.72
2014年11月20日 2052.75
2014年11月21日 2063.50
2014年11月24日 2069.41
2014年11月25日 2067.03
dtype: float64

You can also easily fetch the meta data about any series

In [6]:
info = fred.get_series_info('PAYEMS')
info['title']
Out[6]:
'All Employees: Total nonfarm'

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.

In [7]:
personal_income_series = fred.search_by_release(175, limit=5, order_by='popularity', sort_order='desc')
In [8]:
personal_income_series['title']
Out[8]:
series id
PCPI06075 Per Capita Personal Income in San Francisco County/city, CA
PCPI06085 Per Capita Personal Income in Santa Clara County, CA
PCPI11001 Per Capita Personal Income in the District of Columbia
PCPI36061 Per Capita Personal Income in New York County, NY
PCPI32003 Per Capita Personal Income in Clark County, NV
Name: title, dtype: object
In [9]:
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()
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x10a7341d0>

You can also search by categories. On the FRED website I see that category 101 contains data about Consumer Credit.

In [10]:
df = fred.search_by_category(101, limit=10, order_by='popularity', sort_order='desc')
df['title']
Out[10]:
series id
TOTALSL Total Consumer Credit Owned and Securitized, Outstanding
TERMCBAUTO48NS Finance Rate on Consumer Installment Loans at Commercial...
SLOAS Student Loans Owned and Securitized, Outstanding
TERMCBPER24NS Finance Rate on Personal Loans at Commercial Banks, 24 M...
TERMAFCNCNSA New Car Average Finance Rate at Auto Finance Companies
REVOLSL Total Revolving Credit Owned and Securitized, Outstanding
TERMCBCCALLNS Commercial Bank Interest Rate on Credit Card Plans, All ...
MVLOAS Motor Vehicle Loans Owned and Securitized, Outstanding
NONREVSL Total Nonrevolving Credit Owned and Securitized, Outstan...
TERMCBCCINTNS Commercial Bank Interest Rate on Credit Card Plans, Acco...
Name: title, dtype: object

As a example let's fetch the personal income data. Release 151 looks quite intersting

In [11]:
df = fred.search_by_release(151)
df['title'].head(10)
Out[11]:
series id
AKPCPI Per Capita Personal Income in Alaska
ALPCPI Per Capita Personal Income in Alabama
ARPCPI Per Capita Personal Income in Arkansas
AZPCPI Per Capita Personal Income in Arizona
BEAFWPCPI Per Capita Personal Income in the Far West BEA Region
BEAGLPCPI Per Capita Personal Income in the Great Lakes BEA Region
BEAMEPCPI Per Capita Personal Income in the Mideast BEA Region
BEANEPCPI Per Capita Personal Income in the New England BEA Region
BEAPLPCPI Per Capita Personal Income in the Plains BEA Region
BEARMPCPI Per Capita Personal Income in the Rocky Mountain BEA Region
Name: title, dtype: object

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

In [12]:
state_df = df[~df['title'].str.startswith('Per Capita Personal Income in the')]
In [13]:
len(state_df)
Out[13]:
50
In [14]:
state_df.id.str[:2]
Out[14]:
series id
AKPCPI AK
ALPCPI AL
ARPCPI AR
AZPCPI AZ
CAPCPI CA
COPCPI CO
CTPCPI CT
DEPCPI DE
FLPCPI FL
GAPCPI GA
HIPCPI HI
IAPCPI IA
IDPCPI ID
ILPCPI IL
INPCPI IN
KSPCPI KS
KYPCPI KY
LAPCPI LA
MAPCPI MA
MDPCPI MD
MEPCPI ME
MIPCPI MI
MNPCPI MN
MOPCPI MO
MSPCPI MS
MTPCPI MT
NCPCPI NC
NDPCPI ND
NEPCPI NE
NHPCPI NH
NJPCPI NJ
NMPCPI NM
NVPCPI NV
NYPCPI NY
OHPCPI OH
OKPCPI OK
ORPCPI OR
PAPCPI PA
RIPCPI RI
SCPCPI SC
SDPCPI SD
TNPCPI TN
TXPCPI TX
UTPCPI UT
VAPCPI VA
VTPCPI VT
WAPCPI WA
WIPCPI WI
WVPCPI WV
WYPCPI WY
Name: id, dtype: object

looks good, we got the data series for all 50 states here

In [15]:
income_by_state = {}
for series_id in state_df.index:
 income_by_state[series_id[:2]] = fred.get_series(series_id)
In [16]:
income_by_state = pd.DataFrame(income_by_state)
In [17]:
income_by_state.ix[-1].plot(kind='bar')
plt.title('Per Capita Personal Income by State')
Out[17]:
<matplotlib.text.Text at 0x10bcf0a10>

Comments

Please enable JavaScript to view the comments powered by Disqus.

AltStyle によって変換されたページ (->オリジナル) /