pydeflate 2.2.0__py3-none-any.whl → 2.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydeflate/__init__.py +29 -1
- pydeflate/deflate/get_deflators.py +233 -0
- pydeflate/exchange/get_rates.py +207 -0
- {pydeflate-2.2.0.dist-info → pydeflate-2.3.0.dist-info}/METADATA +133 -1
- {pydeflate-2.2.0.dist-info → pydeflate-2.3.0.dist-info}/RECORD +7 -5
- {pydeflate-2.2.0.dist-info → pydeflate-2.3.0.dist-info}/WHEEL +0 -0
- {pydeflate-2.2.0.dist-info → pydeflate-2.3.0.dist-info}/licenses/LICENSE +0 -0
pydeflate/__init__.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
__author__ = """Jorge Rivera"""
|
|
2
|
-
__version__ = "2.
|
|
2
|
+
__version__ = "2.3.0"
|
|
3
3
|
|
|
4
4
|
from pydeflate.deflate.deflators import (
|
|
5
5
|
imf_cpi_deflate,
|
|
@@ -10,6 +10,15 @@ from pydeflate.deflate.deflators import (
|
|
|
10
10
|
wb_gdp_deflate,
|
|
11
11
|
wb_gdp_linked_deflate,
|
|
12
12
|
)
|
|
13
|
+
from pydeflate.deflate.get_deflators import (
|
|
14
|
+
get_imf_cpi_deflators,
|
|
15
|
+
get_imf_cpi_e_deflators,
|
|
16
|
+
get_imf_gdp_deflators,
|
|
17
|
+
get_oecd_dac_deflators,
|
|
18
|
+
get_wb_cpi_deflators,
|
|
19
|
+
get_wb_gdp_deflators,
|
|
20
|
+
get_wb_gdp_linked_deflators,
|
|
21
|
+
)
|
|
13
22
|
from pydeflate.deflate.legacy_deflate import deflate
|
|
14
23
|
from pydeflate.exchange.exchangers import (
|
|
15
24
|
imf_exchange,
|
|
@@ -17,6 +26,12 @@ from pydeflate.exchange.exchangers import (
|
|
|
17
26
|
wb_exchange,
|
|
18
27
|
wb_exchange_ppp,
|
|
19
28
|
)
|
|
29
|
+
from pydeflate.exchange.get_rates import (
|
|
30
|
+
get_imf_exchange_rates,
|
|
31
|
+
get_oecd_dac_exchange_rates,
|
|
32
|
+
get_wb_exchange_rates,
|
|
33
|
+
get_wb_ppp_rates,
|
|
34
|
+
)
|
|
20
35
|
from pydeflate.pydeflate_config import set_data_dir, setup_logger
|
|
21
36
|
|
|
22
37
|
from pydeflate.context import (
|
|
@@ -60,11 +75,24 @@ __all__ = [
|
|
|
60
75
|
"wb_cpi_deflate",
|
|
61
76
|
"wb_gdp_deflate",
|
|
62
77
|
"wb_gdp_linked_deflate",
|
|
78
|
+
# Get deflators functions
|
|
79
|
+
"get_imf_cpi_deflators",
|
|
80
|
+
"get_imf_cpi_e_deflators",
|
|
81
|
+
"get_imf_gdp_deflators",
|
|
82
|
+
"get_oecd_dac_deflators",
|
|
83
|
+
"get_wb_cpi_deflators",
|
|
84
|
+
"get_wb_gdp_deflators",
|
|
85
|
+
"get_wb_gdp_linked_deflators",
|
|
63
86
|
# Exchange functions
|
|
64
87
|
"imf_exchange",
|
|
65
88
|
"oecd_dac_exchange",
|
|
66
89
|
"wb_exchange",
|
|
67
90
|
"wb_exchange_ppp",
|
|
91
|
+
# Get exchange rates functions
|
|
92
|
+
"get_imf_exchange_rates",
|
|
93
|
+
"get_oecd_dac_exchange_rates",
|
|
94
|
+
"get_wb_exchange_rates",
|
|
95
|
+
"get_wb_ppp_rates",
|
|
68
96
|
# Configuration
|
|
69
97
|
"set_pydeflate_path",
|
|
70
98
|
"setup_logger",
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
|
|
5
|
+
from pydeflate.core.api import BaseDeflate
|
|
6
|
+
from pydeflate.core.source import DAC, IMF, WorldBank
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _generate_get_deflator_docstring(source_name: str, price_kind: str) -> str:
|
|
10
|
+
"""Generate docstring for each get deflator function."""
|
|
11
|
+
return (
|
|
12
|
+
f"Get deflator data from {source_name} ({price_kind}) without requiring user data.\n\n"
|
|
13
|
+
f"This function returns a DataFrame containing deflator values for the specified parameters.\n\n"
|
|
14
|
+
"Args:\n"
|
|
15
|
+
" base_year (int): The base year for calculating deflation adjustments.\n"
|
|
16
|
+
" source_currency (str, optional): The source currency code. Defaults to 'USA'.\n"
|
|
17
|
+
" target_currency (str, optional): The target currency code. Defaults to 'USA'.\n"
|
|
18
|
+
" countries (list[str] | None, optional): List of country codes to include. If None, returns all. Defaults to None.\n"
|
|
19
|
+
" years (list[int] | range | None, optional): List or range of years to include. If None, returns all. Defaults to None.\n"
|
|
20
|
+
" use_source_codes (bool, optional): Use source-specific entity codes. Defaults to False.\n"
|
|
21
|
+
" to_current (bool, optional): Get deflators for constant-to-current conversion. Defaults to False.\n"
|
|
22
|
+
" include_components (bool, optional): Include price_deflator, exchange_deflator, and exchange_rate columns. Defaults to False.\n"
|
|
23
|
+
" update_deflators (bool, optional): Update the deflator data before retrieval. Defaults to False.\n\n"
|
|
24
|
+
"Returns:\n"
|
|
25
|
+
" pd.DataFrame: DataFrame with columns:\n"
|
|
26
|
+
" - iso_code (or entity_code if use_source_codes=True): Country/entity identifier\n"
|
|
27
|
+
" - year: Year\n"
|
|
28
|
+
" - deflator: The combined deflator value\n"
|
|
29
|
+
" - price_deflator (if include_components=True): The price deflator component\n"
|
|
30
|
+
" - exchange_deflator (if include_components=True): The exchange rate deflator component\n"
|
|
31
|
+
" - exchange_rate (if include_components=True): The exchange rate\n"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _get_deflator(deflator_source_cls, price_kind):
|
|
36
|
+
"""Decorator to create get_deflator wrappers with specific deflator source and price kind."""
|
|
37
|
+
|
|
38
|
+
def decorator(func):
|
|
39
|
+
@wraps(func)
|
|
40
|
+
def wrapper(
|
|
41
|
+
*,
|
|
42
|
+
base_year: int,
|
|
43
|
+
source_currency: str = "USA",
|
|
44
|
+
target_currency: str = "USA",
|
|
45
|
+
countries: list[str] | None = None,
|
|
46
|
+
years: list[int] | range | None = None,
|
|
47
|
+
use_source_codes: bool = False,
|
|
48
|
+
to_current: bool = False,
|
|
49
|
+
include_components: bool = False,
|
|
50
|
+
update_deflators: bool = False,
|
|
51
|
+
):
|
|
52
|
+
# Validate input parameters
|
|
53
|
+
if not isinstance(base_year, int):
|
|
54
|
+
raise ValueError("The 'base_year' parameter must be an integer.")
|
|
55
|
+
|
|
56
|
+
# Initialize the deflator source
|
|
57
|
+
source = deflator_source_cls(update=update_deflators)
|
|
58
|
+
|
|
59
|
+
# Create a deflator object
|
|
60
|
+
deflator = BaseDeflate(
|
|
61
|
+
base_year=base_year,
|
|
62
|
+
deflator_source=source,
|
|
63
|
+
exchange_source=source,
|
|
64
|
+
source_currency=source_currency,
|
|
65
|
+
target_currency=target_currency,
|
|
66
|
+
price_kind=price_kind,
|
|
67
|
+
use_source_codes=use_source_codes,
|
|
68
|
+
to_current=to_current,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
# Get the pydeflate data
|
|
72
|
+
data = deflator.pydeflate_data.copy()
|
|
73
|
+
|
|
74
|
+
# Determine the entity column based on use_source_codes
|
|
75
|
+
entity_col = "pydeflate_entity_code" if use_source_codes else "pydeflate_iso3"
|
|
76
|
+
|
|
77
|
+
# Filter by countries if specified
|
|
78
|
+
if countries is not None:
|
|
79
|
+
data = data[data[entity_col].isin(countries)]
|
|
80
|
+
|
|
81
|
+
# Filter by years if specified
|
|
82
|
+
if years is not None:
|
|
83
|
+
if isinstance(years, range):
|
|
84
|
+
years = list(years)
|
|
85
|
+
data = data[data["pydeflate_year"].isin(years)]
|
|
86
|
+
|
|
87
|
+
# Select columns to return
|
|
88
|
+
columns_to_keep = [entity_col, "pydeflate_year", "pydeflate_deflator"]
|
|
89
|
+
|
|
90
|
+
if include_components:
|
|
91
|
+
# Add component columns
|
|
92
|
+
price_col = f"pydeflate_{price_kind}"
|
|
93
|
+
columns_to_keep.extend(
|
|
94
|
+
[price_col, "pydeflate_EXCHANGE_D", "pydeflate_EXCHANGE"]
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Keep only the specified columns
|
|
98
|
+
result = data[columns_to_keep].copy()
|
|
99
|
+
|
|
100
|
+
# Rename columns to user-friendly names
|
|
101
|
+
rename_map = {
|
|
102
|
+
entity_col: "entity_code" if use_source_codes else "iso_code",
|
|
103
|
+
"pydeflate_year": "year",
|
|
104
|
+
"pydeflate_deflator": "deflator",
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if include_components:
|
|
108
|
+
rename_map.update(
|
|
109
|
+
{
|
|
110
|
+
f"pydeflate_{price_kind}": "price_deflator",
|
|
111
|
+
"pydeflate_EXCHANGE_D": "exchange_deflator",
|
|
112
|
+
"pydeflate_EXCHANGE": "exchange_rate",
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
result = result.rename(columns=rename_map)
|
|
117
|
+
|
|
118
|
+
# Reset index
|
|
119
|
+
result = result.reset_index(drop=True)
|
|
120
|
+
|
|
121
|
+
return result
|
|
122
|
+
|
|
123
|
+
wrapper.__doc__ = _generate_get_deflator_docstring(
|
|
124
|
+
deflator_source_cls.__name__, price_kind
|
|
125
|
+
)
|
|
126
|
+
return wrapper
|
|
127
|
+
|
|
128
|
+
return decorator
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@_get_deflator(DAC, "NGDP_D")
|
|
132
|
+
def get_oecd_dac_deflators(
|
|
133
|
+
*,
|
|
134
|
+
base_year: int,
|
|
135
|
+
source_currency: str = "USA",
|
|
136
|
+
target_currency: str = "USA",
|
|
137
|
+
countries: list[str] | None = None,
|
|
138
|
+
years: list[int] | range | None = None,
|
|
139
|
+
use_source_codes: bool = False,
|
|
140
|
+
to_current: bool = False,
|
|
141
|
+
include_components: bool = False,
|
|
142
|
+
update_deflators: bool = False,
|
|
143
|
+
) -> pd.DataFrame: ...
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@_get_deflator(WorldBank, "NGDP_D")
|
|
147
|
+
def get_wb_gdp_deflators(
|
|
148
|
+
*,
|
|
149
|
+
base_year: int,
|
|
150
|
+
source_currency: str = "USA",
|
|
151
|
+
target_currency: str = "USA",
|
|
152
|
+
countries: list[str] | None = None,
|
|
153
|
+
years: list[int] | range | None = None,
|
|
154
|
+
use_source_codes: bool = False,
|
|
155
|
+
to_current: bool = False,
|
|
156
|
+
include_components: bool = False,
|
|
157
|
+
update_deflators: bool = False,
|
|
158
|
+
) -> pd.DataFrame: ...
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@_get_deflator(WorldBank, "NGDP_DL")
|
|
162
|
+
def get_wb_gdp_linked_deflators(
|
|
163
|
+
*,
|
|
164
|
+
base_year: int,
|
|
165
|
+
source_currency: str = "USA",
|
|
166
|
+
target_currency: str = "USA",
|
|
167
|
+
countries: list[str] | None = None,
|
|
168
|
+
years: list[int] | range | None = None,
|
|
169
|
+
use_source_codes: bool = False,
|
|
170
|
+
to_current: bool = False,
|
|
171
|
+
include_components: bool = False,
|
|
172
|
+
update_deflators: bool = False,
|
|
173
|
+
) -> pd.DataFrame: ...
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@_get_deflator(WorldBank, "CPI")
|
|
177
|
+
def get_wb_cpi_deflators(
|
|
178
|
+
*,
|
|
179
|
+
base_year: int,
|
|
180
|
+
source_currency: str = "USA",
|
|
181
|
+
target_currency: str = "USA",
|
|
182
|
+
countries: list[str] | None = None,
|
|
183
|
+
years: list[int] | range | None = None,
|
|
184
|
+
use_source_codes: bool = False,
|
|
185
|
+
to_current: bool = False,
|
|
186
|
+
include_components: bool = False,
|
|
187
|
+
update_deflators: bool = False,
|
|
188
|
+
) -> pd.DataFrame: ...
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
@_get_deflator(IMF, "NGDP_D")
|
|
192
|
+
def get_imf_gdp_deflators(
|
|
193
|
+
*,
|
|
194
|
+
base_year: int,
|
|
195
|
+
source_currency: str = "USA",
|
|
196
|
+
target_currency: str = "USA",
|
|
197
|
+
countries: list[str] | None = None,
|
|
198
|
+
years: list[int] | range | None = None,
|
|
199
|
+
use_source_codes: bool = False,
|
|
200
|
+
to_current: bool = False,
|
|
201
|
+
include_components: bool = False,
|
|
202
|
+
update_deflators: bool = False,
|
|
203
|
+
) -> pd.DataFrame: ...
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@_get_deflator(IMF, "PCPI")
|
|
207
|
+
def get_imf_cpi_deflators(
|
|
208
|
+
*,
|
|
209
|
+
base_year: int,
|
|
210
|
+
source_currency: str = "USA",
|
|
211
|
+
target_currency: str = "USA",
|
|
212
|
+
countries: list[str] | None = None,
|
|
213
|
+
years: list[int] | range | None = None,
|
|
214
|
+
use_source_codes: bool = False,
|
|
215
|
+
to_current: bool = False,
|
|
216
|
+
include_components: bool = False,
|
|
217
|
+
update_deflators: bool = False,
|
|
218
|
+
) -> pd.DataFrame: ...
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@_get_deflator(IMF, "PCPIE")
|
|
222
|
+
def get_imf_cpi_e_deflators(
|
|
223
|
+
*,
|
|
224
|
+
base_year: int,
|
|
225
|
+
source_currency: str = "USA",
|
|
226
|
+
target_currency: str = "USA",
|
|
227
|
+
countries: list[str] | None = None,
|
|
228
|
+
years: list[int] | range | None = None,
|
|
229
|
+
use_source_codes: bool = False,
|
|
230
|
+
to_current: bool = False,
|
|
231
|
+
include_components: bool = False,
|
|
232
|
+
update_deflators: bool = False,
|
|
233
|
+
) -> pd.DataFrame: ...
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
|
|
5
|
+
from pydeflate.core.api import BaseExchange
|
|
6
|
+
from pydeflate.core.source import DAC, IMF, WorldBank, WorldBankPPP
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _generate_get_rates_docstring(source_name: str) -> str:
|
|
10
|
+
"""Generate docstring for each get exchange rates function."""
|
|
11
|
+
return (
|
|
12
|
+
f"Get exchange rate data from {source_name} without requiring user data.\n\n"
|
|
13
|
+
f"This function returns a DataFrame containing exchange rates for the specified parameters.\n\n"
|
|
14
|
+
"Args:\n"
|
|
15
|
+
" source_currency (str, optional): The source currency code. Defaults to 'USA'.\n"
|
|
16
|
+
" target_currency (str, optional): The target currency code. Defaults to 'USA'.\n"
|
|
17
|
+
" countries (list[str] | None, optional): List of country codes to include. If None, returns all. Defaults to None.\n"
|
|
18
|
+
" years (list[int] | range | None, optional): List or range of years to include. If None, returns all. Defaults to None.\n"
|
|
19
|
+
" use_source_codes (bool, optional): Use source-specific entity codes. Defaults to False.\n"
|
|
20
|
+
" update_rates (bool, optional): Update the exchange rate data before retrieval. Defaults to False.\n\n"
|
|
21
|
+
"Returns:\n"
|
|
22
|
+
" pd.DataFrame: DataFrame with columns:\n"
|
|
23
|
+
" - iso_code (or entity_code if use_source_codes=True): Country/entity identifier\n"
|
|
24
|
+
" - year: Year\n"
|
|
25
|
+
" - exchange_rate: Exchange rate from source to target currency\n"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _get_exchange_rates(exchange_source_cls, **fixed_params):
|
|
30
|
+
"""Decorator to create get_exchange_rates wrappers with specific source."""
|
|
31
|
+
|
|
32
|
+
def decorator(func):
|
|
33
|
+
@wraps(func)
|
|
34
|
+
def wrapper(
|
|
35
|
+
*,
|
|
36
|
+
source_currency: str = "USA",
|
|
37
|
+
target_currency: str = "USA",
|
|
38
|
+
countries: list[str] | None = None,
|
|
39
|
+
years: list[int] | range | None = None,
|
|
40
|
+
use_source_codes: bool = False,
|
|
41
|
+
update_rates: bool = False,
|
|
42
|
+
):
|
|
43
|
+
# Apply fixed parameters - no validation needed since these are internally set
|
|
44
|
+
if "target_currency" in fixed_params:
|
|
45
|
+
target_currency = fixed_params["target_currency"]
|
|
46
|
+
|
|
47
|
+
# Initialize the exchange source
|
|
48
|
+
if exchange_source_cls.__name__ == "WorldBankPPP":
|
|
49
|
+
source = exchange_source_cls(
|
|
50
|
+
update=update_rates,
|
|
51
|
+
from_lcu=False if source_currency == "USA" else True,
|
|
52
|
+
)
|
|
53
|
+
source_currency = "LCU" if source_currency == "USA" else source_currency
|
|
54
|
+
else:
|
|
55
|
+
source = exchange_source_cls(update=update_rates)
|
|
56
|
+
|
|
57
|
+
# Create an exchange object
|
|
58
|
+
exchange = BaseExchange(
|
|
59
|
+
exchange_source=source,
|
|
60
|
+
source_currency=source_currency,
|
|
61
|
+
target_currency=target_currency,
|
|
62
|
+
use_source_codes=use_source_codes,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
# Get the exchange rate data
|
|
66
|
+
data = exchange.pydeflate_data.copy()
|
|
67
|
+
|
|
68
|
+
# Determine the entity column based on use_source_codes
|
|
69
|
+
entity_col = "pydeflate_entity_code" if use_source_codes else "pydeflate_iso3"
|
|
70
|
+
|
|
71
|
+
# Filter by countries if specified
|
|
72
|
+
if countries is not None:
|
|
73
|
+
data = data[data[entity_col].isin(countries)]
|
|
74
|
+
|
|
75
|
+
# Filter by years if specified
|
|
76
|
+
if years is not None:
|
|
77
|
+
if isinstance(years, range):
|
|
78
|
+
years = list(years)
|
|
79
|
+
data = data[data["pydeflate_year"].isin(years)]
|
|
80
|
+
|
|
81
|
+
# Select and rename columns
|
|
82
|
+
result = data[[entity_col, "pydeflate_year", "pydeflate_EXCHANGE"]].copy()
|
|
83
|
+
result = result.rename(
|
|
84
|
+
columns={
|
|
85
|
+
entity_col: "entity_code" if use_source_codes else "iso_code",
|
|
86
|
+
"pydeflate_year": "year",
|
|
87
|
+
"pydeflate_EXCHANGE": "exchange_rate",
|
|
88
|
+
}
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
# Reset index
|
|
92
|
+
result = result.reset_index(drop=True)
|
|
93
|
+
|
|
94
|
+
return result
|
|
95
|
+
|
|
96
|
+
wrapper.__doc__ = _generate_get_rates_docstring(exchange_source_cls.__name__)
|
|
97
|
+
return wrapper
|
|
98
|
+
|
|
99
|
+
return decorator
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@_get_exchange_rates(DAC)
|
|
103
|
+
def get_oecd_dac_exchange_rates(
|
|
104
|
+
*,
|
|
105
|
+
source_currency: str = "USA",
|
|
106
|
+
target_currency: str = "USA",
|
|
107
|
+
countries: list[str] | None = None,
|
|
108
|
+
years: list[int] | range | None = None,
|
|
109
|
+
use_source_codes: bool = False,
|
|
110
|
+
update_rates: bool = False,
|
|
111
|
+
) -> pd.DataFrame: ...
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@_get_exchange_rates(WorldBank)
|
|
115
|
+
def get_wb_exchange_rates(
|
|
116
|
+
*,
|
|
117
|
+
source_currency: str = "USA",
|
|
118
|
+
target_currency: str = "USA",
|
|
119
|
+
countries: list[str] | None = None,
|
|
120
|
+
years: list[int] | range | None = None,
|
|
121
|
+
use_source_codes: bool = False,
|
|
122
|
+
update_rates: bool = False,
|
|
123
|
+
) -> pd.DataFrame: ...
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def get_wb_ppp_rates(
|
|
127
|
+
*,
|
|
128
|
+
source_currency: str = "USA",
|
|
129
|
+
countries: list[str] | None = None,
|
|
130
|
+
years: list[int] | range | None = None,
|
|
131
|
+
use_source_codes: bool = False,
|
|
132
|
+
update_rates: bool = False,
|
|
133
|
+
) -> pd.DataFrame:
|
|
134
|
+
"""Get PPP exchange rate data from WorldBankPPP without requiring user data.
|
|
135
|
+
|
|
136
|
+
This function returns a DataFrame containing PPP exchange rates for the specified parameters.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
source_currency (str, optional): The source currency code. Defaults to 'USA'.
|
|
140
|
+
countries (list[str] | None, optional): List of country codes to include. If None, returns all. Defaults to None.
|
|
141
|
+
years (list[int] | range | None, optional): List or range of years to include. If None, returns all. Defaults to None.
|
|
142
|
+
use_source_codes (bool, optional): Use source-specific entity codes. Defaults to False.
|
|
143
|
+
update_rates (bool, optional): Update the exchange rate data before retrieval. Defaults to False.
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
pd.DataFrame: DataFrame with columns:
|
|
147
|
+
- iso_code (or entity_code if use_source_codes=True): Country/entity identifier
|
|
148
|
+
- year: Year
|
|
149
|
+
- exchange_rate: PPP exchange rate
|
|
150
|
+
"""
|
|
151
|
+
# Initialize the exchange source
|
|
152
|
+
source = WorldBankPPP(
|
|
153
|
+
update=update_rates,
|
|
154
|
+
from_lcu=False if source_currency == "USA" else True,
|
|
155
|
+
)
|
|
156
|
+
source_currency_internal = "LCU" if source_currency == "USA" else source_currency
|
|
157
|
+
|
|
158
|
+
# Create an exchange object with PPP as target
|
|
159
|
+
exchange = BaseExchange(
|
|
160
|
+
exchange_source=source,
|
|
161
|
+
source_currency=source_currency_internal,
|
|
162
|
+
target_currency="PPP",
|
|
163
|
+
use_source_codes=use_source_codes,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# Get the exchange rate data
|
|
167
|
+
data = exchange.pydeflate_data.copy()
|
|
168
|
+
|
|
169
|
+
# Determine the entity column based on use_source_codes
|
|
170
|
+
entity_col = "pydeflate_entity_code" if use_source_codes else "pydeflate_iso3"
|
|
171
|
+
|
|
172
|
+
# Filter by countries if specified
|
|
173
|
+
if countries is not None:
|
|
174
|
+
data = data[data[entity_col].isin(countries)]
|
|
175
|
+
|
|
176
|
+
# Filter by years if specified
|
|
177
|
+
if years is not None:
|
|
178
|
+
if isinstance(years, range):
|
|
179
|
+
years = list(years)
|
|
180
|
+
data = data[data["pydeflate_year"].isin(years)]
|
|
181
|
+
|
|
182
|
+
# Select and rename columns
|
|
183
|
+
result = data[[entity_col, "pydeflate_year", "pydeflate_EXCHANGE"]].copy()
|
|
184
|
+
result = result.rename(
|
|
185
|
+
columns={
|
|
186
|
+
entity_col: "entity_code" if use_source_codes else "iso_code",
|
|
187
|
+
"pydeflate_year": "year",
|
|
188
|
+
"pydeflate_EXCHANGE": "exchange_rate",
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
# Reset index
|
|
193
|
+
result = result.reset_index(drop=True)
|
|
194
|
+
|
|
195
|
+
return result
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
@_get_exchange_rates(IMF)
|
|
199
|
+
def get_imf_exchange_rates(
|
|
200
|
+
*,
|
|
201
|
+
source_currency: str = "USA",
|
|
202
|
+
target_currency: str = "USA",
|
|
203
|
+
countries: list[str] | None = None,
|
|
204
|
+
years: list[int] | range | None = None,
|
|
205
|
+
use_source_codes: bool = False,
|
|
206
|
+
update_rates: bool = False,
|
|
207
|
+
) -> pd.DataFrame: ...
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pydeflate
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.3.0
|
|
4
4
|
Summary: Package to convert current prices figures to constant prices and vice versa
|
|
5
5
|
Author: Jorge Rivera
|
|
6
6
|
Author-email: Jorge Rivera <Jorge.Rivera@one.org>
|
|
@@ -28,6 +28,7 @@ Description-Content-Type: text/markdown
|
|
|
28
28
|
[](https://pypi.python.org/pypi/pydeflate)
|
|
29
29
|
[](https://github.com/psf/black)
|
|
30
30
|
[](https://pepy.tech/project/pydeflate)
|
|
31
|
+
[](https://jm-rivera.github.io/pydeflate/)
|
|
31
32
|
|
|
32
33
|
**pydeflate** is a Python package to:
|
|
33
34
|
- Convert current price data to constant prices.
|
|
@@ -36,6 +37,8 @@ Description-Content-Type: text/markdown
|
|
|
36
37
|
|
|
37
38
|
When converting to or from constant prices, it takes into account changes in prices and exchange rates over time. This allows for accurate comparisons across years, countries, and currencies.
|
|
38
39
|
|
|
40
|
+
📚 **[Read the full documentation →](https://jm-rivera.github.io/pydeflate/)**
|
|
41
|
+
|
|
39
42
|
## Important Note
|
|
40
43
|
|
|
41
44
|
**pydeflate v2 has recently been released. It includes api changes which break backwards-compatibility**. While a version of the `deflate` function is still available, it is now deprecated and will be removed in future versions. Please use the new deflator functions for improved simplicity, clarity and performance.
|
|
@@ -58,6 +61,11 @@ When converting to or from constant prices, it takes into account changes in pri
|
|
|
58
61
|
- [Currency Conversion](#currency-conversion)
|
|
59
62
|
- [Example](#example-currency-conversion)
|
|
60
63
|
|
|
64
|
+
- [Getting Deflators and Exchange Rates Directly](#getting-deflators-and-exchange-rates-directly)
|
|
65
|
+
- [Getting Deflators](#getting-deflators)
|
|
66
|
+
- [Getting Exchange Rates](#getting-exchange-rates)
|
|
67
|
+
- [Common Use Cases](#common-use-cases)
|
|
68
|
+
|
|
61
69
|
- [Example: Using Source-Specific Codes](#example-using-source-specific-codes)
|
|
62
70
|
|
|
63
71
|
- [Data Sources and Method Options](#data-sources-and-method-options)
|
|
@@ -182,6 +190,111 @@ df_can = oecd_dac_exchange(
|
|
|
182
190
|
)
|
|
183
191
|
```
|
|
184
192
|
|
|
193
|
+
## Getting Deflators and Exchange Rates Directly
|
|
194
|
+
|
|
195
|
+
**New in v2.3.0**: You can now retrieve deflator and exchange rate data directly as DataFrames, without needing to provide your own data. This is useful for inspecting deflators, analyzing trends, or pre-computing values for later use.
|
|
196
|
+
|
|
197
|
+
### Getting Deflators
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from pydeflate import get_imf_gdp_deflators, set_pydeflate_path
|
|
201
|
+
|
|
202
|
+
# Specify the path where deflator data will be saved
|
|
203
|
+
set_pydeflate_path("path/to/data/folder")
|
|
204
|
+
|
|
205
|
+
# Get deflators for specific countries and years
|
|
206
|
+
deflators = get_imf_gdp_deflators(
|
|
207
|
+
base_year=2015,
|
|
208
|
+
source_currency="USA",
|
|
209
|
+
target_currency="EUR",
|
|
210
|
+
countries=["USA", "FRA", "GBR"], # Optional: filter specific countries
|
|
211
|
+
years=range(2010, 2024), # Optional: filter specific years
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# Returns a DataFrame with columns: iso_code, year, deflator
|
|
215
|
+
print(deflators.head())
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
You can also get the individual components that make up the deflator:
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
# Include price deflator, exchange deflator, and exchange rate components
|
|
222
|
+
deflators_detailed = get_imf_gdp_deflators(
|
|
223
|
+
base_year=2015,
|
|
224
|
+
source_currency="USA",
|
|
225
|
+
target_currency="EUR",
|
|
226
|
+
include_components=True, # Adds price_deflator, exchange_deflator, exchange_rate columns
|
|
227
|
+
)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Available Get Deflator Functions
|
|
231
|
+
|
|
232
|
+
- `get_imf_gdp_deflators`: Get IMF GDP deflators
|
|
233
|
+
- `get_imf_cpi_deflators`: Get IMF CPI deflators
|
|
234
|
+
- `get_imf_cpi_e_deflators`: Get IMF end-of-period CPI deflators
|
|
235
|
+
- `get_wb_gdp_deflators`: Get World Bank GDP deflators
|
|
236
|
+
- `get_wb_gdp_linked_deflators`: Get World Bank linked GDP deflators
|
|
237
|
+
- `get_wb_cpi_deflators`: Get World Bank CPI deflators
|
|
238
|
+
- `get_oecd_dac_deflators`: Get OECD DAC deflators
|
|
239
|
+
|
|
240
|
+
### Getting Exchange Rates
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
from pydeflate import get_imf_exchange_rates
|
|
244
|
+
|
|
245
|
+
# Get exchange rates for specific currency pairs
|
|
246
|
+
rates = get_imf_exchange_rates(
|
|
247
|
+
source_currency="USD",
|
|
248
|
+
target_currency="EUR",
|
|
249
|
+
countries=["USA", "FRA", "GBR"], # Optional: filter specific countries
|
|
250
|
+
years=range(2010, 2024), # Optional: filter specific years
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
# Returns a DataFrame with columns: iso_code, year, exchange_rate
|
|
254
|
+
print(rates.head())
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Available Get Exchange Rate Functions
|
|
258
|
+
|
|
259
|
+
- `get_imf_exchange_rates`: Get IMF exchange rates
|
|
260
|
+
- `get_wb_exchange_rates`: Get World Bank exchange rates
|
|
261
|
+
- `get_wb_ppp_rates`: Get World Bank PPP conversion rates
|
|
262
|
+
- `get_oecd_dac_exchange_rates`: Get OECD DAC exchange rates
|
|
263
|
+
|
|
264
|
+
### Common Use Cases
|
|
265
|
+
|
|
266
|
+
**Analyzing deflator trends:**
|
|
267
|
+
```python
|
|
268
|
+
import matplotlib.pyplot as plt
|
|
269
|
+
|
|
270
|
+
# Get US GDP deflators over time
|
|
271
|
+
deflators = get_imf_gdp_deflators(
|
|
272
|
+
base_year=2015,
|
|
273
|
+
countries=["USA"],
|
|
274
|
+
years=range(2000, 2024)
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
plt.plot(deflators["year"], deflators["deflator"])
|
|
278
|
+
plt.title("US GDP Deflator (Base Year 2015)")
|
|
279
|
+
plt.show()
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
**Pre-computing deflators for manual calculations:**
|
|
283
|
+
```python
|
|
284
|
+
# Get deflators
|
|
285
|
+
deflators = get_imf_gdp_deflators(
|
|
286
|
+
base_year=2020,
|
|
287
|
+
source_currency="USA",
|
|
288
|
+
target_currency="EUR"
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
# Use in your own calculations
|
|
292
|
+
my_value_2021 = 100 # USD in 2021
|
|
293
|
+
deflator_2021 = deflators[(deflators["iso_code"] == "USA") &
|
|
294
|
+
(deflators["year"] == 2021)]["deflator"].iloc[0]
|
|
295
|
+
constant_value = my_value_2021 / deflator_2021
|
|
296
|
+
```
|
|
297
|
+
|
|
185
298
|
## Example: Using Source-Specific Codes
|
|
186
299
|
|
|
187
300
|
If your data uses source-specific country codes (e.g., DAC codes), set use_source_codes=True and specify the appropriate id_column.
|
|
@@ -383,3 +496,22 @@ This is useful for:
|
|
|
383
496
|
- Customizing logging verbosity
|
|
384
497
|
- Testing with temporary cache directories
|
|
385
498
|
|
|
499
|
+
## Documentation
|
|
500
|
+
|
|
501
|
+
For comprehensive documentation including detailed examples, advanced features, and troubleshooting:
|
|
502
|
+
|
|
503
|
+
**[📚 Full Documentation](https://jm-rivera.github.io/pydeflate/)**
|
|
504
|
+
|
|
505
|
+
The documentation includes:
|
|
506
|
+
- [Getting Started Guide](https://jm-rivera.github.io/pydeflate/getting-started/) - Setup and DataFrame requirements
|
|
507
|
+
- [Deflation Guide](https://jm-rivera.github.io/pydeflate/deflation/) - All deflation methods with examples
|
|
508
|
+
- [Currency Exchange](https://jm-rivera.github.io/pydeflate/exchange/) - Currency conversion examples
|
|
509
|
+
- [Data Sources](https://jm-rivera.github.io/pydeflate/data-sources/) - IMF, World Bank, and OECD DAC comparison
|
|
510
|
+
- [Advanced Topics](https://jm-rivera.github.io/pydeflate/advanced/exceptions/) - Error handling, contexts, plugins, validation
|
|
511
|
+
- [Migration Guide](https://jm-rivera.github.io/pydeflate/migration/) - v1 to v2 migration
|
|
512
|
+
- [FAQ](https://jm-rivera.github.io/pydeflate/faq/) - Common questions and troubleshooting
|
|
513
|
+
|
|
514
|
+
## Contributing
|
|
515
|
+
|
|
516
|
+
Contributions are welcome! Please feel free to submit a Pull Request or open an issue on [GitHub](https://github.com/jm-rivera/pydeflate).
|
|
517
|
+
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
pydeflate/.pydeflate_data/README.md,sha256=atNtUL9dD8G184YSd6juFib8TgEQBcSLogiz99APPVs,25
|
|
2
|
-
pydeflate/__init__.py,sha256=
|
|
2
|
+
pydeflate/__init__.py,sha256=Bp8xuw0h7-bOrtKXMRqxE_PjdZsPGKCP1zthphGSu3U,2747
|
|
3
3
|
pydeflate/cache.py,sha256=jfNilTrzGucdFpaTn6AwQWCFMT5123ZgcYwhDgtsCK8,4755
|
|
4
4
|
pydeflate/constants.py,sha256=-xxH2skAWHHMQxIJSFWOayU-GmRlqowjkIhIzvyUuac,2972
|
|
5
5
|
pydeflate/context.py,sha256=0HBHnaHcpESDbyfZLo-5W_Kg03WI5Itf0LCABa2w08E,6565
|
|
@@ -10,10 +10,12 @@ pydeflate/core/exchange.py,sha256=br6RVgTGa7LW09XemUJZ4Koazf65zuXPQKYKGhS6ROM,85
|
|
|
10
10
|
pydeflate/core/source.py,sha256=dZiMqVdUXD6KQ0RuzR4clb7sCPYcCMZOkg9GhhjqppI,4908
|
|
11
11
|
pydeflate/deflate/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
12
|
pydeflate/deflate/deflators.py,sha256=qd6afJfVNHpNb12xDfS0TGHg5p4ngH5Tb_CWk6555Us,7768
|
|
13
|
+
pydeflate/deflate/get_deflators.py,sha256=rR5lTU_rv2tEPlNEMwGXuUC3vY2fQ5k9MZMvw4T8J5I,8308
|
|
13
14
|
pydeflate/deflate/legacy_deflate.py,sha256=N9tIuKzVOQtSw5QBeJG6Um86nheLG0yXgdvPVZCmrIA,3900
|
|
14
15
|
pydeflate/exceptions.py,sha256=31LzEG0aOT41e6S21NvsR2Lt_pozQmVgonqLwvOcn7I,4711
|
|
15
16
|
pydeflate/exchange/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
17
|
pydeflate/exchange/exchangers.py,sha256=HxKM2cqDEArQplww2d-lm-EAbccSMxEovErpkgTunkk,6721
|
|
18
|
+
pydeflate/exchange/get_rates.py,sha256=-v9QbKWNKGTtsWqSUXD0j0S2qtan6wtXhu3DQN6akSk,7775
|
|
17
19
|
pydeflate/plugins.py,sha256=fZxO79v4cbbHvsYAvstIC7kk2uSPVCiurXqrS2mukqI,8872
|
|
18
20
|
pydeflate/protocols.py,sha256=0jVSynQItjt-yv8HSU19RPQrfo1Vgo7Ws1CQfX5ZIaI,4694
|
|
19
21
|
pydeflate/pydeflate_config.py,sha256=9r8tu-k-XhFMM2x3dQwEjk2dHj-pwdUf6pdvEowbE2I,2725
|
|
@@ -26,7 +28,7 @@ pydeflate/sources/dac.py,sha256=RkO_nFZcp8AeSJNFzdBrGqaL2kLVUeXBHTPO6PkUFy0,3824
|
|
|
26
28
|
pydeflate/sources/imf.py,sha256=Nu8Gm4wT-FdBpTuaYUrzOC2V3BNP9srNOIuQJ9E16_0,6712
|
|
27
29
|
pydeflate/sources/world_bank.py,sha256=1DzDlr0Xe0DA0OUsycjwiq-4yMasnP0TneXEK0VmcRw,7281
|
|
28
30
|
pydeflate/utils.py,sha256=HRVMUuWKS1IpU8lmkvQN1jIMA5-LEFzyEd0ReZSx65k,4252
|
|
29
|
-
pydeflate-2.
|
|
30
|
-
pydeflate-2.
|
|
31
|
-
pydeflate-2.
|
|
32
|
-
pydeflate-2.
|
|
31
|
+
pydeflate-2.3.0.dist-info/licenses/LICENSE,sha256=8ymAThz7Z4JhjqL6vDwTaoq29tFytQRDkoW3rnTGstI,1075
|
|
32
|
+
pydeflate-2.3.0.dist-info/WHEEL,sha256=n2u5OFBbdZvCiUKAmfnY1Po2j3FB_NWfuUlt5WiAjrk,79
|
|
33
|
+
pydeflate-2.3.0.dist-info/METADATA,sha256=dMxbCpdH_ya7gJ4WNrqAfxwVseSd30-Ln1nLtn3jwsI,20555
|
|
34
|
+
pydeflate-2.3.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|