Adjusting Historical Stock Data For Inflation using Python and CPI
Using the package from Palewire quickly adjust historical stock data for effective time series predictions
Are you trying to predict the performance of your favorite stock and are operating on raw historical stock data to build your time series forecasting models? Well, now you can adjust your closing prices without the hassle of cross-referencing the CPI database. Thank Palewire for creating a Python package titled cpi for the ability to inflate values to modern standards.
Let’s demonstrate by pulling a variable stock price say Tesla from Yahoo Finance. I recommend downloading the maximum amount of historical stock price data. Next, make sure you have the cpi package installed.
pip install cpi
Then we can begin our adjustment for inflation using the cpi package. We begin by loading our data and libraries.
import pandas as pd
import cpi
tsla = pd.read_csv("TSLA.csv")
## Convert date column into python datetime
tsla['Date'] = pd.to_datetime(tsla['Date'])
tsla.head()
The columns are [‘Date’, ‘Open’, ‘High’, ‘Low’, ‘Close’, ‘Adj Close’, ‘Volume’]. One would think the Adj Close column would already be the inflation adjusted column, but the values in this stock matched the close throughout the dataset. Let’s…