Skip to content

Commit

Permalink
implementing specific tools
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-rivera committed Nov 9, 2024
1 parent 92760af commit dd9eeb6
Show file tree
Hide file tree
Showing 13 changed files with 514 additions and 78 deletions.
21 changes: 1 addition & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions pydeflate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
__author__ = """Jorge Rivera"""
__version__ = "1.3.10"

from pydeflate.deflate.deflators import (
oecd_dac_deflate,
wb_cpi_deflate,
wb_gdp_deflate,
wb_gdp_linked_deflate,
imf_cpi_deflate,
imf_gdp_deflate,
imf_cpi_e_deflate,
)

from pydeflate.deflate.legacy_deflate import deflate
from pydeflate.exchange.exchangers import oecd_dac_exchange, wb_exchange, imf_exchange
from pydeflate.pydeflate_config import setup_logger


Expand All @@ -14,10 +26,17 @@ def set_pydeflate_path(path):
PYDEFLATE_PATHS.data = Path(path).resolve()


logger = setup_logger("pydeflate")


__all__ = [
"set_pydeflate_path",
"logger",
"oecd_dac_deflate",
"oecd_dac_exchange",
"wb_cpi_deflate",
"wb_gdp_deflate",
"wb_gdp_linked_deflate",
"wb_exchange",
"imf_cpi_deflate",
"imf_gdp_deflate",
"imf_cpi_e_deflate",
"imf_exchange",
"deflate",
]
69 changes: 53 additions & 16 deletions pydeflate/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@
)


def resolve_common_currencies(currency: str, source: str) -> str:
mapping = {
"USD": "USA",
"EUR": "EMU",
"GBP": "GBR",
"JPY": "JPN",
"CAD": "CAN",
}

if source == "DAC":
mapping["EUR"] = "EUI"

return mapping.get(currency, currency)


def _base_operation(
base_obj,
data: pd.DataFrame,
entity_column: str,
year_column: str,
value_column: str,
target_value_column: str | None = None,
year_format: str = "%Y",
year_format: str | None = None,
exchange: bool = False,
reversed_: bool = False,
):
"""Perform deflation or exchange rate adjustment on input data using pydeflate data.
Expand All @@ -35,6 +51,7 @@ def _base_operation(
year_format (str, optional): Format of the year. Defaults to "%Y".
exchange (bool, optional): Whether to perform an exchange rate adjustment (True)
or deflation (False).
reversed_ (bool, optional): If True, perform the operation in reverse.
Returns:
pd.DataFrame: DataFrame with adjusted values and original columns preserved.
Expand All @@ -61,18 +78,16 @@ def _base_operation(

# Flag missing data
flag_missing_pydeflate_data(base_obj._unmatched_data)

# Calculate deflated values
if exchange:
base_obj._merged_data[target_value_column] = (
base_obj._merged_data[value_column]
* base_obj._merged_data["pydeflate_EXCHANGE"]
).round(6)
x = base_obj._merged_data[value_column]
y = base_obj._merged_data[
"pydeflate_EXCHANGE" if exchange else "pydeflate_deflator"
]

# Apply the correct operation based on `exchange` and `reversed`
if (exchange and not reversed_) or (not exchange and reversed_):
base_obj._merged_data[target_value_column] = (x * y).round(6)
else:
base_obj._merged_data[target_value_column] = (
base_obj._merged_data[value_column]
/ base_obj._merged_data["pydeflate_deflator"]
).round(6)
base_obj._merged_data[target_value_column] = (x / y).round(6)

return base_obj._merged_data[cols]

Expand Down Expand Up @@ -101,6 +116,16 @@ def __init__(
use_source_codes (bool, optional): Use source-specific entity codes.
Defaults to False.
"""

# Try to accept common currencies by their country codes
source_currency = resolve_common_currencies(
source_currency, exchange_source.name
)

target_currency = resolve_common_currencies(
target_currency, exchange_source.name
)

self.exchange_rates = Exchange(
source=exchange_source,
source_currency=source_currency,
Expand All @@ -127,7 +152,7 @@ def _merge_pydeflate_data(
data: pd.DataFrame,
entity_column: str,
year_column: str,
year_format: str = "%Y",
year_format: str | None = None,
) -> None:
"""Merge pydeflate exchange rate data into input data by year and entity.
Expand Down Expand Up @@ -164,7 +189,8 @@ def exchange(
year_column: str,
value_column: str,
target_value_column: str | None = None,
year_format: str = "%Y",
year_format: str | None = None,
reversed_: bool = False,
):
"""Apply exchange rate conversion to input data.
Expand All @@ -175,6 +201,7 @@ def exchange(
value_column (str): Column with values to adjust.
target_value_column (str | None, optional): Column for adjusted values. Defaults to `value_column`.
year_format (str, optional): Format of the year. Defaults to "%Y".
reversed_ (bool, optional): If True, perform the operation in reverse. defaults to False.
Returns:
pd.DataFrame: DataFrame with exchange rate-adjusted values.
Expand All @@ -188,6 +215,7 @@ def exchange(
value_column=value_column,
target_value_column=target_value_column,
year_format=year_format,
reversed_=reversed_,
)


Expand Down Expand Up @@ -223,6 +251,15 @@ def __init__(
to_current (bool, optional): If True, adjust to current year values. Defaults to False.
"""

# Try to accept common currencies by their country codes
source_currency = resolve_common_currencies(
source_currency, deflator_source.name
)

target_currency = resolve_common_currencies(
target_currency, deflator_source.name
)

self.exchange_rates = Exchange(
source=exchange_source,
source_currency=source_currency,
Expand Down Expand Up @@ -305,7 +342,7 @@ def _merge_pydeflate_data(
data: pd.DataFrame,
entity_column: str,
year_column: str,
year_format: str = "%Y",
year_format: str | None = None,
) -> None:
"""Merge pydeflate deflator data into the input data by year and entity.
Expand Down Expand Up @@ -342,7 +379,7 @@ def deflate(
year_column: str,
value_column: str,
target_value_column: str | None = None,
year_format: str = "%Y",
year_format: str | None = None,
):
"""Apply deflation adjustment to input data using pydeflate deflator rates.
Expand Down
Loading

0 comments on commit dd9eeb6

Please sign in to comment.