cpi

A Python library that quickly adjusts U.S. dollars for inflation using the Consumer Price Index.

Installation

The library can be installed from the Python Package Index with any of the standard Python installation tools, such as pipenv.

pipenv install cpi

Working with Python

Before you can use it, you need to add the library to your Python script with an import statement.

import cpi
/tmp/ipykernel_1823/1241436912.py:1: StaleDataWarning: CPI data is out of date. To accurately inflate to today's dollars, you must run `cpi.update()`.
  import cpi

Adjusting prices for inflation is as simple as providing a dollar value along with its year of origin to the inflate method.

cpi.inflate(100, 1950)
1264.323651452282

By default, the value is adjusted to the most recent year. Unless otherwise specified, the “CPI-U” index for all urban consumers is used to make the conversion, the method recommended by the U.S. Bureau of Labor Statistics.

If you’d like to adjust to a different year, you can submit it as an integer to the optional to keyword argument.

cpi.inflate(100, 1950, to=1960)
122.82157676348547

You can also adjust month to month. You should submit the months as datetime.date objects.

from datetime import date

cpi.inflate(100, date(1950, 1, 1), to=date(2018, 1, 1))
1054.7531914893618

You can adjust values using any other series published by the BLS as part of its “All Urban Consumers (CU)” survey. They offer more precise measures for different regions and items.

Submit one of the 60 agency-tracked areas to inflate dollars in that region.

cpi.inflate(100, 1950, area="Los Angeles-Long Beach-Anaheim, CA")
1356.89029535865

You can find a complete list in the repository or by running the following command:

cpi.areas.all()[:5]
[<Area: U.S. city average>,
 <Area: Northeast>,
 <Area: New England>,
 <Area: Middle Atlantic>,
 <Area: Midwest>]

You can do the same to inflate the price of 400 specific items lumped into the basket of goods that make up the overall index.

cpi.inflate(100, 1980, items="Housing")
394.78668310727505

You can find a complete list in the repository or by running the following command:

cpi.items.all()[:5]
[<Item: All items - old base>,
 <Item: Purchasing power of the consumer dollar - old base>,
 <Item: All items>,
 <Item: Energy>,
 <Item: All items less food>]

And you can do both together.

cpi.inflate(100, 1980, items="Housing", area="Los Angeles-Long Beach-Anaheim, CA")
438.3440860215054

Each of the 7,800 variations on the CU survey has a unique identifier. If you know which one you want, you can submit it directly.

cpi.inflate(100, 2000, series_id="CUUSS12ASETB01")
231.02132895816243

If you’d like to retrieve the CPI value itself for any year, use the get method.

cpi.get(1950)
24.1

You can also do that by month.

cpi.get(date(1950, 1, 1))
23.5

The same keyword arguments are available.

cpi.get(1980, items="Housing", area="Los Angeles-Long Beach-Anaheim, CA")
83.7

If you’d like to retrieve a particular CPI series for inspection, use the series attribute’s get method. No configuration returns the default series.

cpi.series.get()
<Series: CUUR0000SA0: All items in U.S. city average, all urban consumers, not seasonally adjusted>

Alter the configuration options to retrieve variations based on item, area and other metadata.

cpi.series.get(items="Housing", area="Los Angeles-Long Beach-Anaheim, CA")
<Series: CUURS49ASAH: Housing in Los Angeles-Long Beach-Anaheim, CA, all urban consumers, not seasonally adjusted>

If you know a series’ identifier code, you can submit it directly to get_by_id.

cpi.series.get_by_id("CUURS49ASAH")
<Series: CUURS49ASAH: Housing in Los Angeles-Long Beach-Anaheim, CA, all urban consumers, not seasonally adjusted>

Once retrieved, a series’s complete set of index values is accessible via the indexes property.

series = cpi.series.get(items="Housing", area="Los Angeles-Long Beach-Anaheim, CA")
series.indexes[:5]
[<Index: 1997-01-01 (January): 155.4>,
 <Index: 1997-02-01 (February): 155.6>,
 <Index: 1997-03-01 (March): 155.5>,
 <Index: 1997-04-01 (April): 155.2>,
 <Index: 1997-05-01 (May): 156.1>]

That’s it!

Working with pandas

Using the ‘ apply ‘ method, an inflation-adjusted column can quickly be added to a pandas DataFrame. Here is an example using data tracking the median household income in the United States from The Federal Reserve Bank of St. Louis.

import cpi
import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/palewire/cpi/main/tests/test.csv")
df["ADJUSTED"] = df.apply(
    lambda x: cpi.inflate(x.MEDIAN_HOUSEHOLD_INCOME, x.YEAR), axis=1
)

Working from the command line

The Python package also installs a command-line interface for inflate available on the terminal.

It works the same as the Python library. First, give it a value, then a source year. By default, it is adjusted to its value in the most recent available year.

inflate 100 1950

If you’d like to adjust to a different year, submit it as an integer to the --to option.

inflate 100 1950 --to=1960

You can also adjust month to month. You should submit the months as parseable date strings.

inflate 100 1950-01-01 --to=2018-01-01

Here are all its options.

inflate --help
Usage: inflate [OPTIONS] VALUE YEAR_OR_MONTH

  Returns a dollar value adjusted for inflation.

Options:
  --to TEXT      The year or month to adjust the value to.
  --series_id TEXT  The CPI data series used for the conversion. The default is the CPI-U.
  --help         Show this message and exit.

About our source

The adjustment is made using data provided by The Bureau of Labor Statistics at the U.S. Department of Labor.

Currently, the library only supports inflation adjustments using a series from the “All Urban Consumers (CU)” survey. The so-called “CPI-U” survey is the default, an average of all prices paid by all urban consumers. It has been available since 1913. It is not seasonally adjusted. The BLS identifies the dataset as “CUUR0000SA0.” It is used as the default for most basic inflation calculations. All other series measuring all urban consumers are available by taking advantage of the library’s options. The alternative “Urban Wage Earners and Clerical Workers” survey is not yet available.

Updating the data

Since the BLS routinely releases new CPI values, this library must periodically download the latest data. This library does not do this automatically. You must update the BLS dataset stored alongside the code yourself by running the following method:

cpi.update()

Other resources