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…

• Obtain CPI data from FRED using the python API
• Obtain median household income data for the U.S.
• Adjust the U.S. median household income data using the CPI
• Compare the median household income data (adjusted) in the U.S. with the data adjusted using the CPI.
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


## Get data from FRED

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


## Household Income in the United States

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="")


value
date
2013-01-0153585
2014-01-0153657
2015-01-0156516
2016-01-0159039
2017-01-0161136

data["adjusted"] = [
cpi.inflate(dollers.value, date.year) for date, dollers in data.iterrows()
]


### Comparison before and after adjustment

plt.figure(figsize=(12, 6))
sns.lineplot(
data=data,
x="date",
y="value",
)
plt.legend()


## Real Median Household Income in the United States

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="")


value
date
2013-01-0162425
2014-01-0161468
2015-01-0164631
2016-01-0166657
2017-01-0167571
plt.figure(figsize=(12, 6))
sns.lineplot(data=data_real, x="date", y="value", label="MEHOINUSA672N")