Price Indices Package
This Python package computes various price indices using both unweighted and weighted methods, suitable for different types of data handling needs. It supports calculations via native Python structures as well as pandas DataFrames, facilitating ease of integration into data analysis workflows.
Supported Unweighted Methods: - Jevons, Dutot, Carli, Balk-Mehrhoff-Walsh (BMW) Indices.
Supported Weighted Methods: - Laspeyres, Paasche, Fisher, Törnqvist, Walsh, Sato-Vartia Indices.
The package allows using dictionaries for input data in native Python functions and DataFrames for pandas-enabled functions, with options to specify base and comparison periods. It is distributed under the GNU General Public License v3 or later, ensuring freedom to modify and redistribute the software.
Constants define default DataFrame columns for prices, product IDs, quantities, and time periods, alongside a default normalization value for index calculations.
- price_indices.price_indices.DEFAULT_NORMALIZATION_VAL = 100.0
Default normalization value for indices.
- price_indices.price_indices.PD_DEFAULT_PRICE_COL = 'price'
Default column name for prices in DataFrame.
- price_indices.price_indices.PD_DEFAULT_PRODUCT_ID_COL = 'product_id'
Default column name for product IDs in DataFrame.
- price_indices.price_indices.PD_DEFAULT_QUANTITY_COL = 'quantity'
Default column name for quantities in DataFrame.
- price_indices.price_indices.PD_DEFAULT_TIME_PERIOD_COL = 'time_period'
Default column name for time periods in DataFrame.
- price_indices.price_indices.bmw_index(prices_0, prices_t, normalization_value=100.0)[source]
Calculate the Balk-Mehrhoff-Walsh (BMW) price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
BMW price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.bmw_index_from_df(df, base_period, compared_period, price_col='price', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Balk-Mehrhoff-Walsh (BMW) price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
BMW price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.carli_index(prices_0, prices_t, normalization_value=100.0)[source]
Calculate the Carli price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Carli price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.carli_index_from_df(df, base_period, compared_period, price_col='price', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Carli price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Carli price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.dutot_index(prices_0, prices_t, normalization_value=100.0)[source]
Calculate the Dutot price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Dutot price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.dutot_index_from_df(df, base_period, compared_period, price_col='price', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Dutot price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Dutot price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.fisher_index(prices_0, prices_t, quantities_0, quantities_t, normalization_value=100.0)[source]
Calculate the Fisher price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
quantities_0 (Dict[str, float]) – Quantities of products at the base time period.
quantities_t (Dict[str, float]) – Quantities of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Fisher price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.fisher_index_from_df(df, base_period, compared_period, price_col='price', quantity_col='quantity', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Fisher price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price and quantity data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
quantity_col (str) – The name of the column containing the quantities. Defaults to ‘quantity’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Fisher price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
AttributeError – If the DataFrame does not contain the necessary columns.
- price_indices.price_indices.jevons_index(prices_0, prices_t, normalization_value=100.0)[source]
Calculate the Jevons price index, which is a geometric mean of the price relatives.
The Jevons price index is defined by the formula:
\[P_{J}^{0, t} = \prod_{i \in G_{0, t}} \left( \frac{p_{i}^{t}}{p_{i}^{0}} \right)^{\frac{1}{N_{0, t}}}\]Where: \(G_{0,t}\) is the set of matched products from period 0 and t, \(p_{i}^{0}\) and \(p_{i}^{t}\) are the prices of product \(i\) at the base and compared time periods respectively, and \(N_{0,t}\) is the number of matched products.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Jevons price index, normalized.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.jevons_index_from_df(df, base_period, compared_period, price_col='price', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Jevons price index from a pandas.DataFrame.
The Jevons price index is defined by the formula:
\[P_{J}^{0, t} = \prod_{i \in G_{0, t}} \left( \frac{p_{i}^{t}}{p_{i}^{0}} \right)^{\frac{1}{N_{0, t}}}\]Where: \(G_{0,t}\) is the set of matched products from period 0 and t, \(p_{i}^{0}\) and \(p_{i}^{t}\) are the prices of product \(i\) at the base and compared time periods respectively, and \(N_{0,t}\) is the number of matched products.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Jevons price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.laspeyres_index(prices_0, prices_t, quantities_0, normalization_value=100.0)[source]
Calculate the Laspeyres price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
quantities_0 (Dict[str, float]) – Quantities of products at the base time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Laspeyres price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.laspeyres_index_from_df(df, base_period, compared_period, price_col='price', quantity_col='quantity', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Laspeyres price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price and quantity data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
quantity_col (str) – The name of the column containing the quantities. Defaults to ‘quantity’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Laspeyres price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
AttributeError – If the DataFrame does not contain the necessary columns.
- price_indices.price_indices.paasche_index(prices_0, prices_t, quantities_t, normalization_value=100.0)[source]
Calculate the Paasche price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
quantities_t (Dict[str, float]) – Quantities of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Paasche price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.paasche_index_from_df(df, base_period, compared_period, price_col='price', quantity_col='quantity', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Paasche price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price and quantity data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
quantity_col (str) – The name of the column containing the quantities. Defaults to ‘quantity’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Paasche price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
AttributeError – If the DataFrame does not contain the necessary columns.
- price_indices.price_indices.sato_vartia_index(prices_0, prices_t, quantities_0, quantities_t, normalization_value=100.0)[source]
Calculate the Sato-Vartia price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
quantities_0 (Dict[str, float]) – Quantities of products at the base time period.
quantities_t (Dict[str, float]) – Quantities of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Sato-Vartia price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.sato_vartia_index_from_df(df, base_period, compared_period, price_col='price', quantity_col='quantity', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Sato-Vartia price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price and quantity data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
quantity_col (str) – The name of the column containing the quantities. Defaults to ‘quantity’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Sato-Vartia price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
AttributeError – If the DataFrame does not contain the necessary columns.
- price_indices.price_indices.tornqvist_index(prices_0, prices_t, quantities_0, quantities_t, normalization_value=100.0)[source]
Calculate the Törnqvist price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
quantities_0 (Dict[str, float]) – Quantities of products at the base time period.
quantities_t (Dict[str, float]) – Quantities of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Törnqvist price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.tornqvist_index_from_df(df, base_period, compared_period, price_col='price', quantity_col='quantity', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Törnqvist price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price and quantity data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
quantity_col (str) – The name of the column containing the quantities. Defaults to ‘quantity’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Törnqvist price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
AttributeError – If the DataFrame does not contain the necessary columns.
- price_indices.price_indices.walsh_index(prices_0, prices_t, quantities_0, quantities_t, normalization_value=100.0)[source]
Calculate the Walsh price index.
- Parameters:
prices_0 (Dict[str, float]) – Prices of products at the base time period.
prices_t (Dict[str, float]) – Prices of products at the compared time period.
quantities_0 (Dict[str, float]) – Quantities of products at the base time period.
quantities_t (Dict[str, float]) – Quantities of products at the compared time period.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Walsh price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
- price_indices.price_indices.walsh_index_from_df(df, base_period, compared_period, price_col='price', quantity_col='quantity', product_id_col='product_id', time_period_col='time_period', normalization_value=100.0)[source]
Calculate the Walsh price index from a pandas.DataFrame.
- Parameters:
df (pd.DataFrame) – The DataFrame containing the price and quantity data.
base_period (int) – The base time period.
compared_period (int) – The compared time period.
price_col (str) – The name of the column containing the prices. Defaults to ‘price’.
quantity_col (str) – The name of the column containing the quantities. Defaults to ‘quantity’.
product_id_col (str) – The name of the column containing the product IDs. Defaults to ‘product_id’.
time_period_col (str) – The name of the column containing the time periods. Defaults to ‘time_period’.
normalization_value (float) – The value to normalize the index to. Defaults to 100.
- Returns:
Walsh price index.
- Return type:
float
- Raises:
ValueError – If no matched products are found.
AttributeError – If the DataFrame does not contain the necessary columns.