How To Correct For Time Value Of Money With Cpi Python
The Easiest Elbow room to Adjust Your Data for Pompousness in Python
With the cpi library, you bum do it with a single line of code!
Inflation — the word we get wind in the news pretty much on a daily basis. We know that, long story short, inflation means that our money is worth inferior over fourth dimension. But how much less you said it to align the values for ostentatiousness? I will answer those questions in this article aside showing how to work with inflation in Python. Simply first…
A bit routine of possibility
I won't spend much time writing about t he political economy theory for inflation and its consequences, atomic number 3 this is a matter for a much thirster clause with a different focal point. To define ostentatiousness in one sentence — it is the general originate in the price story of an economy over a period of time. It substance that when the worldwide price level rises, with each unit of currency (be IT dollars, euros, etc.) we can buy less units of certain goods or services. And that is the reducing in the purchasing power of money that was mentioned in front.
We measure inflation with the inflation rate, which is the annualized percentage change in a general price level, most commonly the CPI (California Personality Inventory).
We need to define ii many terms that will hold the analysis easy to follow. The first unmatchable is current dollars. Information technology refers to the value from the period of time when the monetary value was in reality registered. Or alternatively, it is the value not adjusted for inflation. The sec condition is the real dollars, that is, the value after adjusting for rising prices.
The general formula used for adjusting the prices for inflation exploitation 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!
Good example in Python
Setup
As always, we consequence the needed libraries.
For this article, we volition adjust the dollar values using the cpi library, which makes the process more easier than manually downloading the CPI data and adjusting the values. One thing to keep in brain is that this depository library works only with U.S. Dollars. For otherwise currencies, you bequeath give to adjust the data manually or find an alternative library.
While importation the libraries, we mightiness realize the next word of advice:
StaleDataWarning: CPI information is out of see. To accurately inflate to today's dollars, you must run `cpi.update()`. It is rather obvious, we motivation to run the following bid to get the latest CPI information.
cpi.update() We are instantly set up to adjust the dollar values for inflation.
Basic operations with cpi
The virtually important function of the cpi program library is inflate, which we use to adjust the value expressed in rife dollars for inflation. In the undermentioned snip, we supply the clam prize and the year in which it was filmed.
cost-of-living index.inflate(100, 2000) # 150.2967
The outcome means that if something cost 100$ in 2000, thanks to inflation it would be worth ~150$ in 2022. That is because the occasion's default settings will adjust that value to the most recent full year (at the second of written material, it is 2022) using the Cost-of-living index-U (Consumer Price Index for All Cityfied Consumer) index, which is the suggested default by the Bureau of Labor Statistics. You can read more about the index present.
We bum also define the butt year to which we need to adjust the economic value.
consumer price index.inflate(100, 2000, to=2010) # 126.6295
We are not classified to using days. For month-to-month adjustments, we tin simply provide datetime.go out objects as arguments.
Lastly, we can also quite easily fix the value of the CPI index using the get method.
cpi.get(2020) # 258.811
Combining cpi with pandas
That was already quite accessible, however, in most cases we volition not be interested in getting single values, but adjusting the full series of values stored in a pandas Series/DataFrame.
First, we need some information. For this toy example, we terminate use the Median Household Income in the United States from the FRED database. Information technology's always goodish to double-check what kinds of adjustments were already practical to the data. We pot interpret in the following image that the income series is in occurrent 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 information technology for adjusting the prevailing dollar values.
Unfortunately, there is no shapely-in functionality to iterate work with pandas DataFrames. However, we can easily do IT ourselves using the apply method acting.
Down the stairs we can inspect the median household income in the United States of America in both contemporary and material dollars. We can understandably see that the values converge to the selfsame one, only do non ordinate completely. That is because the income time series ends in the year 2022, while we are adjusting to the year 2022.
Takeaways
- inflation is the rise in the average price level of an economy over a period of time.
- current dollars represent the value in the clock when it was recorded, while real dollars are adjusted for pretentiousness.
- we can use the
CPIlibrary 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 sack poke out to Maine on Twitter or in the comments.
If you are interested in learning about how to use Python for denary finance, you might want to check out Quantra (disclaimer: an affiliate link), which offers a variety of different courses along the topic.
If you liked this clause, you might also equal concerned in nonpareil of the following:
References
[1] U.S. Census Bureau, Median Household Income in the United States [MEHOINUSA646N], retrieved from FRED, Federal Reserve Bank of Gateway to the West; https://fred.stlouisfed.org/series/MEHOINUSA646N, July 12, 2022.
How To Correct For Time Value Of Money With Cpi Python
Source: https://towardsdatascience.com/the-easiest-way-to-adjust-your-data-for-inflation-in-python-365490c03969
Posted by: kaplanfourpece.blogspot.com

0 Response to "How To Correct For Time Value Of Money With Cpi Python"
Post a Comment