Skip to content

Commit

Permalink
Create cpi.py
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbailey committed Aug 22, 2018
1 parent 7c3db28 commit be895e8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rail/cpi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pandas as pd


class CPI(object):
"""A class to retrieve United States CPI data and calculate inflation"""

def __init__(self) -> None:
self.cpi = pd.read_csv('https://download.bls.gov/pub/time.series/cu/cu.data.0.Current', sep='\t')

def inflation(self, from_year: int, to_year: int) -> float:
cpi_from = self.cpi[(self.cpi['series_id'] == 'CUUS0000SA0 ') & (self.cpi['year'] == from_year) & (self.cpi['period'] == 'S01')]['value']
cpi_to = self.cpi[(self.cpi['series_id'] == 'CUUS0000SA0 ') & (self.cpi['year'] == to_year) & (self.cpi['period'] == 'S01')]['value']
return cpi_to.values[0] / cpi_from.values[0]

0 comments on commit be895e8

Please sign in to comment.