Inflation Adjustment
The src.inflation
module provides tools to adjust monetary values from different years into "real terms" against a common baseline year. This is essential for meaningful historical comparisons of policy impacts.
The module uses historical Consumer Price Index (CPI) data for New Zealand, fetched from the World Bank's data API.
Key Functions
adjust_for_inflation()
This is the main function for performing inflation adjustments.
src.inflation.adjust_for_inflation(data, base_year, target_year, columns_to_adjust)
Adjusts specified monetary columns of a DataFrame from a target year's value to a base year's value.
For example, to convert 1990 dollars to 2023 dollars, base_year=2023 and target_year=1990.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The pandas DataFrame to adjust. |
required |
base_year
|
int
|
The year to adjust the currency to (e.g., 2023). |
required |
target_year
|
int
|
The year the currency is currently in (e.g., 1990). |
required |
columns_to_adjust
|
list[str]
|
A list of column names in the DataFrame to adjust. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The DataFrame with the specified columns adjusted for inflation. |
Source code in src/inflation.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
Usage Example
A full, working example of how to use this functionality can be found in the examples/run_inflation_adjustment.py
script. Here is a brief overview of the process:
- Prepare your data: Start with a pandas DataFrame that contains the monetary data you wish to adjust.
- Define parameters: Specify your
base_year
(the year you want to convert to), yourtarget_year
(the year the data is currently in), and a list of thecolumns_to_adjust
. - Call the function: Pass your DataFrame and parameters to the
adjust_for_inflation()
function.
# Example snippet from examples/run_inflation_adjustment.py
df_adjusted = adjust_for_inflation(
data=my_dataframe,
base_year=2022,
target_year=1990,
columns_to_adjust=["income", "assets"],
)
Data Caching
The first time you use this module, it will download the necessary CPI data from the World Bank API. To improve performance on subsequent runs, this data is automatically cached in a .cache
directory within the src
folder. You can safely delete this directory if you need to force a re-download of the data.