The Easiest Way to Adjust Your Data for Inflation in Python | by Eryk Lewinson | Towards Data Science

Inflation — the word we hear in the news pretty much on a daily basis. We know that, long story short, inflation means that our money is worth less over time. But how much less and how to adjust the values for inflation? I will answer those questions in this article by showing how to work with inflation in Python. But first...
I won’t spend much time writing about the economics theory for inflation and its consequences, as this is a topic for a much longer article with a different focus. To define inflation in one sentence — it is the general rise in the price level of an economy over a period of time. It means that when the general price level rises, with each unit of currency (be it dollars, euros, etc.) we can buy fewer units of certain goods or services. And that is the reduction in the purchasing power of money that was mentioned before.
We measure inflation with the inflation rate, which is the annualized percentage change in a general price index, most commonly the consumer price index (CPI).
We need to define two more terms that will make the analysis easy to follow. The first one is current dollars. It refers to the value from the time period when the monetary value was actually registered. Or alternatively, it is the value not adjusted for inflation. The second term is the real dollars, that is, the value after adjusting for inflation.
The general formula used for adjusting the prices for inflation using the CPI is:
real_dollars = (current_dollars * cpi_new) / cpi_old
And that was is for theory, let’s move onto the hands-on part!
As always, we import the required libraries.
For this article, we will adjust the dollar values using the cpi library, which makes the process much easier than manually downloading the CPI data and adjusting the values. One thing to keep in mind is that this library works only with US Dollars. For other currencies, you will have to adjust the data manually or find an alternative library.
While importing the libraries, we might see the following warning:
StaleDataWarning: CPI data is out of date. To accurately inflate to today’s dollars, you must run `cpi.update()`.
It is rather self-explanatory, we need to run the following command to get the latest CPI data.
cpi.update()
We are now ready to adjust the dollar values for inflation.
The most important function of the cpi library is inflate, which we use to adjust the value expressed in current dollars for inflation. In the following snippet, we provide the dollar value and the year in which it was recorded.
cpi.inflate(100, 2000)# 150.2967
The outcome means that if something cost 100$ in 2000, thanks to inflation it would be worth ~150$ in 2020. That is because the function’s default settings will adjust that value to the most recent full year (at the moment of writing, it is 2020) using the CPI-U (Consumer Price Index for All Urban Consumer) index, which is the recommended default by the Bureau of Labor Statistics. You can read more about the index here.
We can also specify the target year to which we want to adjust the value.
cpi.inflate(100, 2000, to=2010)# 126.6295
We are not restricted to using years. For month-to-month adjustments, we can simply provide datetime.date objects as arguments.
Lastly, we can also quite easily get the value of the CPI index using the get method.
cpi.get(2020)# 258.811
That was already quite handy, however, in most cases we will not be interested in getting single values, but adjusting the entire series of values stored in a pandas Series/DataFrame.
First, we need some data. For this toy example, we can use the Median Household Income in the United States from the FRED database. It’s always good to double-check what kinds of adjustments were already applied to the data. We can see in the following image that the income series is in current dollars.
We download the data from FRED’s website and load it to Python using pandas. We also do some renaming to keep the names meaningful and create an extra column with the year extracted from the date — we will use it for adjusting the current dollar values.
Unfortunately, there is no built-in functionality to iterate work with pandas DataFrames. However, we can easily do it ourselves using the apply method.
Below we can inspect the median household income in the US in both current and real dollars. We can clearly see that the values converge to the same one, but do not align completely. That is because the income time series ends in the year 2019, while we are adjusting to the year 2020.
inflation is the rise in the average price level of an economy over a period of time.
current dollars represent the value in the time when it was recorded, while real dollars are adjusted for inflation.
we can use the cpi library to easily adjust for inflation (USD only).
You can find the code used for this article on my GitHub. Also, any constructive feedback is welcome. You can reach out to me on Twitter or in the comments.
If you are interested in learning about how to use Python for quantitative finance, you might want to check out Quantra (disclaimer: an affiliate link), which offers a variety of different courses on the topic.
If you liked this article, you might also be interested in one of the following:
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
[1] U.S. Census Bureau, Median Household Income in the United States [MEHOINUSA646N], retrieved from FRED, Federal Reserve Bank of St. Louis; https://fred.stlouisfed.org/series/MEHOINUSA646N, July 12, 2021.