Sometimes data affected by the value of money need to be adjusted before analysis. Here we will use the Consumer Price Index (CPI) to adjust time series data for inflation. Specifically, in this page, I’ll try to…
import os
import cpi
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from full_fred.fred import Fred
from datetime import date
cpi.update() # Make sure you do it
Obtain the necessary data from FRED. The method of data acquisition is explained in a separate video, but it is necessary to issue an API key to access the data from python.
# FRED_API_KEY = os.getenv('FRED_API_KEY')
fred = Fred()
print(f"FRED API key is set to an environment variable:{fred.env_api_key_found()}")
def get_fred_data(name, start="2013-01-01", end=""):
df = fred.get_series_df(name)[["date", "value"]].copy()
df["date"] = pd.to_datetime(df["date"])
df["value"] = pd.to_numeric(df["value"], errors="coerce")
df = df.set_index("date")
if end == "":
df = df.loc[f"{start}":]
else:
df = df.loc[f"{start}":f"{end}"]
return df
FRED API key is set to an environment variable:True
Let’s check the data for Median Household Income in the United States (MEHOINUSA646N). This data is in units of
Units: Current Dollars, Not Seasonally Adjusted
and this data is not adjusted for inflation or seasonally adjusted. We adjust this household data using the inflation rate.
data = get_fred_data("MEHOINUSA646N", start="2013-01-01", end="")
data.head()
value | |
---|---|
date | |
2013-01-01 | 53585 |
2014-01-01 | 53657 |
2015-01-01 | 56516 |
2016-01-01 | 59039 |
2017-01-01 | 61136 |
data["adjusted"] = [
cpi.inflate(dollers.value, date.year) for date, dollers in data.iterrows()
]
plt.figure(figsize=(12, 6))
sns.lineplot(
data=data,
x="date",
y="value",
label="Current Dollars, Not Seasonally Adjusted(MEHOINUSA646N)",
)
sns.lineplot(data=data, x="date", y="adjusted", label="MEHOINUSA646N adjusted")
plt.legend()
Adjusted data are provided in Real Median Household Income in the United States (MEHOINUSA672N). Compare the values in MEHOINUSA672N with the inflation-adjusted data (values in the adjusted
column in the data
) from earlier. The unadjusted values, adjusted using the Consumer Price Index (CPI), are expected to match the adjusted values (MEHOINUSA672N) almost exactly.
data_real = get_fred_data("MEHOINUSA672N", start="2013-01-01", end="")
data_real.head()
value | |
---|---|
date | |
2013-01-01 | 62425 |
2014-01-01 | 61468 |
2015-01-01 | 64631 |
2016-01-01 | 66657 |
2017-01-01 | 67571 |
plt.figure(figsize=(12, 6))
sns.lineplot(data=data_real, x="date", y="value", label="MEHOINUSA672N")
sns.lineplot(data=data, x="date", y="adjusted", label="MEHOINUSA646N adjusted")
for t, v in data_real.iterrows():
plt.text(t, v[0] - 500, f"{v[0]:.2f}")
for t, v in data.iterrows():
plt.text(t, v[1] + 500, f"{v[1]:.2f}")
plt.legend()