Getting FRED Data
The first thing we need to do is install pandas_datareader.
! pip install pandas_datareader
Now let’s import our libraries and set up start and end dates.
import pandas_datareader as pdr
import datetime
start = datetime.datetime(1955, 1, 1)
end = datetime.datetime(2017, 9, 1)
FRED, which you can find
here
, contains large amounts of data. FEDFUNDS is the name for the federal funds data series, and CPIAUCSL is the name for the consumer price index data series.
Using fred.FredReader() in pandas_datareader, we can access data series by specifying an array of series names, start date, and end date.
reader = pdr.fred.FredReader(["FEDFUNDS","CPIAUCSL"],start,end)
Now, to get the data we use read() and it will return a dataframe.
df = reader.read()
df
Pandas allows us to plot by calling .plot() and specifying an argument “kind”. So if we want a line chart, we would say kind=”line” as so…
import matplotlib.pyplot as plt
df["FEDFUNDS"].plot(kind="line")
plt.show()
Let’s also plot the consumer price index.
df["CPIAUCSL"].plot(kind="line")
plt.show()
Comparing the consumer price index with the Fed Funds Rate won’t tell us much. One is a rate, and one is a measure of overall price levels. What we can do is turn the CPI into a percentage change, to become the growth rate period over period for the CPI (which is what inflation is). Pandas has pct_change() for this purpose.
df["CPIAUCSL"].pct_change().plot(kind="line")
plt.show()
Let’s set the column equal to this.
df["CPIAUCSL"] = df["CPIAUCSL"].pct_change()
df.dropna(inplace=True)
Challenge