Skip to content

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
def adjust_for_inflation(
    data: pd.DataFrame,
    base_year: int,
    target_year: int,
    columns_to_adjust: list[str],
) -> pd.DataFrame:
    """
    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.

    Args:
        data: The pandas DataFrame to adjust.
        base_year: The year to adjust the currency to (e.g., 2023).
        target_year: The year the currency is currently in (e.g., 1990).
        columns_to_adjust: A list of column names in the DataFrame to adjust.

    Returns:
        The DataFrame with the specified columns adjusted for inflation.
    """
    cpi_data = get_cpi_data()

    if not cpi_data:
        print("CPI data is not available. Cannot perform inflation adjustment.")
        return data

    if base_year not in cpi_data:
        raise ValueError(f"CPI data not available for base year: {base_year}")
    if target_year not in cpi_data:
        raise ValueError(f"CPI data not available for target year: {target_year}")

    cpi_base = cpi_data[base_year]
    cpi_target = cpi_data[target_year]

    if cpi_target == 0:
        raise ValueError(f"CPI for target year {target_year} is zero, cannot adjust.")

    adjustment_factor = cpi_base / cpi_target

    print(f"Adjusting from {target_year} to {base_year} with factor: {adjustment_factor:.4f}")

    adjusted_data = data.copy()
    for col in columns_to_adjust:
        if col in adjusted_data.columns:
            adjusted_data[col] = adjusted_data[col] * adjustment_factor
        else:
            print(f"Warning: Column '{col}' not found in DataFrame. Skipping.")

    return adjusted_data

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:

  1. Prepare your data: Start with a pandas DataFrame that contains the monetary data you wish to adjust.
  2. Define parameters: Specify your base_year (the year you want to convert to), your target_year (the year the data is currently in), and a list of the columns_to_adjust.
  3. 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.