Using the FRED API to get and display key macro series

FRED, a database maintained by the Federal Reserve Bank of St. Louis, is a great, realiable source of macroecononomic and financial data. One can manually access it, download the needed data and manipulate it, but Python makes the process much easier and replicable. For one very current illustration, let us see using recent data if we can see evidence that changes in presidential administrations have a noticeable impact on the economy. Given the current climate, this notebook will just plot numbers. You can decide for yourself whether there is any inflection of note in those series.

The first step is to install the FRED API.

In [2]:
pip install fredapi 
Requirement already satisfied: fredapi in c:\users\equintin\anaconda3\lib\site-packages (0.4.2)
Requirement already satisfied: pandas in c:\users\equintin\anaconda3\lib\site-packages (from fredapi) (1.0.5)
Requirement already satisfied: python-dateutil>=2.6.1 in c:\users\equintin\anaconda3\lib\site-packages (from pandas->fredapi) (2.8.1)
Requirement already satisfied: numpy>=1.13.3 in c:\users\equintin\anaconda3\lib\site-packages (from pandas->fredapi) (1.18.5)
Requirement already satisfied: pytz>=2017.2 in c:\users\equintin\anaconda3\lib\site-packages (from pandas->fredapi) (2020.1)
Requirement already satisfied: six>=1.5 in c:\users\equintin\anaconda3\lib\site-packages (from python-dateutil>=2.6.1->pandas->fredapi) (1.15.0)
Note: you may need to restart the kernel to use updated packages.
In [3]:
# My go-to modules

import pandas as pd # pandas is excellent for creating and manipulating dataframes, R-style
import numpy as np # great for simulations, we may not use for running regressions here
import matplotlib.pyplot as plt #graphing module with matlab-like properties
%matplotlib inline 
import requests # to make requests to webpages (like mine)
import statsmodels.api as sm # module full of standard statistical and econometric models, including OLS and time-series stuff
from IPython.display import Latex # to be able to display latex in python command cells
from import DataReader # datereader downloads from all kinds of places including yahoo and google finance
import datetime

Next we will download the FRED API, ask the FED for an ID (go here, and enter it.

In [4]:
from fredapi import Fred 
fred = Fred(api_key='Insert your API key here')

Next we build the dataframes we need.

In [5]:
macrodata = fred.get_series('GDPC1', observation_start='2009-3-1') # Seasonally adjusted real GDP
macrodata=macrodata.to_frame() # converts a series type into a dataframe, a much easier object to play with imo
macrodata = macrodata.rename(columns= {0: 'Real GDP'})'date'
macrodata['Employment(NF)']=fred.get_series('PAYEMS', observation_start='2009-3-1') # seasonally ajusted non-fram employment in thousands
macrodata['Unemployment']=fred.get_series('UNRATE', observation_start='2009-3-1') # seasonally ajusted unemployment rate in percent

#for the stock market, because dates are unlikely to match with above, we create a different dataframe

stockdata=fred.get_series('SP500', observation_start='2009-3-1')  # only starts in 2010 at Fred for some reason
stockdata=stockdata.to_frame() # converts a series type into a dataframe, a much easier object to play with imo
stockdata = stockdata.rename(columns= {0: 'S&P500'})'date'

Now we plot the results and try to find inflections in the series at any particular date. A formal break test could help us with that, but it hardly seems necessary. (Don't squint too hard.) The impact of COVID, of course, is enormous, btu that's a different conversation.

In [44]:
fig, axs  = plt.subplots(2, 2,figsize=(12, 8))
fig.suptitle('US Economy since march 2009')


# plt.subplot(221)

axs[0][0].plot(x,macrodata['Real GDP'])
axs[0][0].set_title("Real GDP, SA")
axs[0,0].set_ylabel("2012 $bn")

axs[0][1].set_title("Non-farm employment, SA")

axs[1][0].set_title("Unemployment, SA")