pydeflate 1.4.1__py3-none-any.whl → 2.0.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 +25 -17
- pydeflate/core/api.py +404 -0
- pydeflate/core/deflator.py +171 -0
- pydeflate/core/exchange.py +237 -0
- pydeflate/core/source.py +54 -0
- pydeflate/deflate/deflators.py +228 -0
- pydeflate/deflate/legacy_deflate.py +109 -0
- pydeflate/exchange/__init__.py +0 -0
- pydeflate/exchange/exchangers.py +147 -0
- pydeflate/pydeflate_config.py +25 -16
- pydeflate/sources/__init__.py +0 -0
- pydeflate/sources/common.py +278 -0
- pydeflate/sources/dac.py +137 -0
- pydeflate/sources/imf.py +203 -0
- pydeflate/sources/world_bank.py +186 -0
- pydeflate/utils.py +55 -22
- {pydeflate-1.4.1.dist-info → pydeflate-2.0.0.dist-info}/LICENSE +1 -1
- pydeflate-2.0.0.dist-info/METADATA +287 -0
- pydeflate-2.0.0.dist-info/RECORD +25 -0
- {pydeflate-1.4.1.dist-info → pydeflate-2.0.0.dist-info}/WHEEL +1 -1
- pydeflate/deflate/deflate.py +0 -324
- pydeflate/deflate/deflator.py +0 -78
- pydeflate/get_data/deflate_data.py +0 -70
- pydeflate/get_data/exchange_data.py +0 -371
- pydeflate/get_data/imf_data.py +0 -76
- pydeflate/get_data/oecd_data.py +0 -146
- pydeflate/get_data/wb_data.py +0 -75
- pydeflate/tools/__init__.py +0 -2
- pydeflate/tools/exchange.py +0 -171
- pydeflate/tools/update_data.py +0 -69
- pydeflate-1.4.1.dist-info/METADATA +0 -305
- pydeflate-1.4.1.dist-info/RECORD +0 -22
- /pydeflate/{get_data → core}/__init__.py +0 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pydeflate
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Package to convert current prices figures to constant prices and vice versa
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Jorge Rivera
|
|
7
|
+
Author-email: jorge.rivera@one.org
|
|
8
|
+
Requires-Python: >=3.10,<4.0
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Natural Language :: English
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Requires-Dist: hdx-python-country (>=3.8.1,<4.0.0)
|
|
20
|
+
Requires-Dist: imf-reader (>=1.1.0,<2.0.0)
|
|
21
|
+
Requires-Dist: oda-reader (>=1.0.0,<2.0.0)
|
|
22
|
+
Requires-Dist: pandas (>=2,<3)
|
|
23
|
+
Requires-Dist: pyarrow (>14)
|
|
24
|
+
Requires-Dist: wbgapi (>=1.0.12,<2.0.0)
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# pydeflate
|
|
28
|
+
|
|
29
|
+
[](https://pypi.python.org/pypi/pydeflate)
|
|
30
|
+
[](https://github.com/psf/black)
|
|
31
|
+
[](https://pepy.tech/project/pydeflate)
|
|
32
|
+
|
|
33
|
+
**pydeflate** is a Python package to:
|
|
34
|
+
- Convert current price data to constant prices.
|
|
35
|
+
- Convert constant price data to current prices.
|
|
36
|
+
- Convert data from one currency to another (in both current and constant prices).
|
|
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.
|
|
39
|
+
|
|
40
|
+
## Important Note
|
|
41
|
+
|
|
42
|
+
**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.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# Table of Contents
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
- [Installation](#installation)
|
|
49
|
+
|
|
50
|
+
- [Basic Usage](#basic-usage)
|
|
51
|
+
- [Setting Up pydeflate](#setting-up-pydeflate)
|
|
52
|
+
- [DataFrame Requirements](#dataframe-requirements)
|
|
53
|
+
|
|
54
|
+
- [Converting Current to Constant Prices](#converting-current-to-constant-prices)
|
|
55
|
+
- [Example](#example-convert-current-to-constant-prices)
|
|
56
|
+
|
|
57
|
+
- [Available Deflator Functions](#available-deflator-functions)
|
|
58
|
+
|
|
59
|
+
- [Currency Conversion](#currency-conversion)
|
|
60
|
+
- [Example](#example-currency-conversion)
|
|
61
|
+
|
|
62
|
+
- [Example: Using Source-Specific Codes](#example-using-source-specific-codes)
|
|
63
|
+
|
|
64
|
+
- [Data Sources and Method Options](#data-sources-and-method-options)
|
|
65
|
+
- [International Monetary Fund](#international-monetary-fund)
|
|
66
|
+
- [World Bank](#world-bank)
|
|
67
|
+
- [OECD Development Assistance Committee](#oecd-development-assistance-committee)
|
|
68
|
+
- [Sources](#sources)
|
|
69
|
+
|
|
70
|
+
- [Handling Missing Data](#handling-missing-data)
|
|
71
|
+
|
|
72
|
+
- [Updating Underlying Data](#updating-underlying-data)
|
|
73
|
+
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
Install pydeflate using pip:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pip install pydeflate --upgrade
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Basic Usage
|
|
83
|
+
|
|
84
|
+
### Setting Up pydeflate
|
|
85
|
+
|
|
86
|
+
Before using pydeflate, you must specify where the deflator and exchange data should be saved. This only needs to be done once per script or notebook.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from pydeflate import set_pydeflate_path
|
|
90
|
+
|
|
91
|
+
# Specify the path where deflator and exchange data will be saved
|
|
92
|
+
set_pydeflate_path("path/to/data/folder")
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### DataFrame requirements
|
|
96
|
+
You need to provide a pandas DataFrame in order to convert data with `pydeflate`. The DataFrame must have at least the following columns:
|
|
97
|
+
- **An `id_column`**: you must specify its name using the `id_column` parameter. By default, it expects `ISO3` country codes. Previous versions of pydeflate used to convert data automatically, but that could inadvertently introduce errors by mis-identifying countries. You can use tools like `bblocks`, `hdx-python-country` or `country-converter` to help you add `ISO3` codes to your data. If you're working with data from the same source as the one you're using in `pydeflate`, you can also set `use_source_codes=True`. That allows you to use the same encoding as the source data (e.g., DAC codes, IMF entity codes).
|
|
98
|
+
- **A `year_column`**: which can be a string, integer, or datetime. This is needed in order to match the data to the right deflator or exchange rate. By default, pydeflate assumes that the year column is named `year`. You can change this by setting the `year_column` parameter. If the optional parameter `year_format` is not set, pydeflate will try to infer the format of the year column. You can also provide a `year_format` as a string, to specify the format of your data's year column.
|
|
99
|
+
- **A `value_column`**: which contains the data to be converted. By default, pydeflate assumes that the value column is named `value`. You can change this by setting the `value_column` parameter. The type of the value column must be numeric (int, float).
|
|
100
|
+
|
|
101
|
+
## Converting Current to Constant Prices
|
|
102
|
+
|
|
103
|
+
Pydeflate includes multiple sources and methods to deflate data. They all work in a very similar way. For this example, we will use the IMF GDP deflator and exchange rates data.
|
|
104
|
+
|
|
105
|
+
### Example: Convert Current to Constant Prices
|
|
106
|
+
In this example, we first import the `imf_gdp_deflate` function and create a sample DataFrame. We then convert the data to constant 2015 EUR prices using the IMF GDP deflators and exchange rates.
|
|
107
|
+
|
|
108
|
+
Note that both the `source_currency` and the `target_currency` are specified using the ISO3 country codes of the country whose currency is being used. Note that either can also be specified as `LCU` which stands for 'local currency units', or the local currency for each individual country, instead applying a single currency to all values. For convenience `pydeflate` also accepts the currency codes of certain countries (like `USD` in place of `USA`, `EUR` in place of any country that uses the euro, `GBP` in place of `GBR`, etc).
|
|
109
|
+
|
|
110
|
+
If the required data to perform the conversion is not available, pydeflate will download it from the source and save it in the specified data folder. If the stored data is older than 50 days, `pydeflate` will inform you and encourage you to set the `update_data` parameter to `True`.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from pydeflate import imf_gdp_deflate, set_pydeflate_path
|
|
114
|
+
import pandas as pd
|
|
115
|
+
|
|
116
|
+
# Specify the path where deflator and exchange data will be saved
|
|
117
|
+
set_pydeflate_path("path/to/data/folder")
|
|
118
|
+
|
|
119
|
+
# Example data in current USD prices
|
|
120
|
+
data = {
|
|
121
|
+
'iso_code': ['FRA', 'USA', 'GTM'],
|
|
122
|
+
'year': [2017, 2017, 2017],
|
|
123
|
+
'value': [50, 100, 200]
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
df = pd.DataFrame(data)
|
|
127
|
+
|
|
128
|
+
# Convert to constant EUR prices (base year 2015)
|
|
129
|
+
df_constant = imf_gdp_deflate(
|
|
130
|
+
data=df,
|
|
131
|
+
base_year=2015,
|
|
132
|
+
source_currency="USA", # Data is in USD
|
|
133
|
+
target_currency="FRA", # Convert to Euro
|
|
134
|
+
id_column="iso_code", # must be ISO3 code
|
|
135
|
+
year_column="year", # Can be string, integer or datetime
|
|
136
|
+
value_column="value", # Column to be converted
|
|
137
|
+
target_value_column="value_constant" # It could also be the same as value_column
|
|
138
|
+
)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Available Deflator Functions
|
|
142
|
+
|
|
143
|
+
- `imf_gdp_deflate`: Uses GDP deflators and exchange rates from the IMF World Economic Outlook.
|
|
144
|
+
- `imf_cpi_deflate`: Uses Consumer Price Index and exchange rates data from the IMF World Economic Outlook.
|
|
145
|
+
- `imf_cpi_e_deflate`: Uses end-of-period Consumer Price Index and exchange rates data from the IMF World Economic Outlook.
|
|
146
|
+
- `wb_gdp_deflate`: Uses GDP deflators and exchange rates from the World Bank.
|
|
147
|
+
- `wb_gdp_linked_deflate`: Uses the World Bank’s linked GDP deflator and exchange rates data.
|
|
148
|
+
- `wb_cpi_deflate`: Uses Consumer Price Index and exchange rate data from the World Bank.
|
|
149
|
+
- `oecd_dac_deflate`: Uses the OECD DAC deflator series (prices and exchange rates).
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
## Currency Conversion
|
|
154
|
+
Pydeflate includes multiple sources for currency exchange. They all work in a very similar way, using yearly exchange rates. For this example, we will use the OECD DAC exchange rates.
|
|
155
|
+
|
|
156
|
+
### Example: Currency Conversion
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from pydeflate import oecd_dac_exchange, set_pydeflate_path
|
|
160
|
+
import pandas as pd
|
|
161
|
+
|
|
162
|
+
# Specify the path where deflator and exchange data will be saved
|
|
163
|
+
set_pydeflate_path("path/to/data/folder")
|
|
164
|
+
|
|
165
|
+
# Example data in current local currency units
|
|
166
|
+
data = {
|
|
167
|
+
'iso_code': ['GBR', 'CAN', 'JPN'],
|
|
168
|
+
'year': [2011, 2015, 2015],
|
|
169
|
+
'value': [100, 100, 100]
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
df = pd.DataFrame(data)
|
|
173
|
+
|
|
174
|
+
# Convert from local currency (e.g GBP, CAD, JPY in this case) to Canadian Dollars
|
|
175
|
+
df_can = oecd_dac_exchange(
|
|
176
|
+
data=df,
|
|
177
|
+
source_currency="LCU", # Local currency units
|
|
178
|
+
target_currency="CAN", # Convert to Canadian Dollars (can also use 'CAD')
|
|
179
|
+
id_column="iso_code", # must be ISO3 code
|
|
180
|
+
year_column="year", # Can be string, integer or datetime
|
|
181
|
+
value_column="value", # Column to be converted
|
|
182
|
+
target_value_column="value_can" # It could also be the same as value_column
|
|
183
|
+
)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Example: Using Source-Specific Codes
|
|
187
|
+
|
|
188
|
+
If your data uses source-specific country codes (e.g., DAC codes), set use_source_codes=True and specify the appropriate id_column.
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from pydeflate import oecd_dac_deflate, set_pydeflate_path
|
|
192
|
+
import pandas as pd
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
# Specify the path where deflator and exchange data will be saved
|
|
196
|
+
set_pydeflate_path("path/to/data/folder")
|
|
197
|
+
|
|
198
|
+
# Example data with DAC codes
|
|
199
|
+
data = {
|
|
200
|
+
'dac_code': [302, 4, 4],
|
|
201
|
+
'year': [2010, 2016, 2018],
|
|
202
|
+
'value': [100, 100, 100]
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
df = pd.DataFrame(data)
|
|
206
|
+
|
|
207
|
+
# Convert using DAC deflators and DAC codes
|
|
208
|
+
df_constant = oecd_dac_deflate(
|
|
209
|
+
data=df,
|
|
210
|
+
base_year=2016,
|
|
211
|
+
source_currency="USA", # Data is in USD
|
|
212
|
+
target_currency="LCU", # Convert to local currency units
|
|
213
|
+
id_column="dac_code", # DAC codes
|
|
214
|
+
use_source_codes=True, # Use source-specific codes
|
|
215
|
+
year_column="year", # Can be string, integer or datetime
|
|
216
|
+
value_column="value", # Column to be converted
|
|
217
|
+
target_value_column="value_constant" # It could also be the same as value_column
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Data Sources and Method Options
|
|
223
|
+
|
|
224
|
+
Pydeflate uses data on price/gdp deflators and exchange rates from various sources. Each source offers different options for deflators and exchange rates.
|
|
225
|
+
|
|
226
|
+
For all sources, Exchange rates between two non USD currency pairs are derived from
|
|
227
|
+
the LCU to USD exchange rates selected.
|
|
228
|
+
|
|
229
|
+
### International Monetary Fund
|
|
230
|
+
The IMF provides estimates where data is not available, including for several
|
|
231
|
+
years into the future. Using these price deflators, combined with the corresponding
|
|
232
|
+
exchange rates, can also allow users to convert data to constant prices for future years.
|
|
233
|
+
|
|
234
|
+
Deflator Functions:
|
|
235
|
+
- `imf_gdp_deflate`: Uses GDP deflators.
|
|
236
|
+
- `imf_cpi_deflate`: Uses Consumer Price Index data.
|
|
237
|
+
- `imf_cpi_e_deflate`: Uses end-of-period Consumer Price Index data.
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
Exchange Function:
|
|
241
|
+
- `imf_exchange`: Uses exchange rates derived from the IMF’s data.
|
|
242
|
+
|
|
243
|
+
Notes:
|
|
244
|
+
- IMF data includes estimates for future years, allowing conversion to constant prices for future dates.
|
|
245
|
+
- Exchange rates are derived from the IMF’s implied rates.
|
|
246
|
+
|
|
247
|
+
### World Bank
|
|
248
|
+
|
|
249
|
+
Deflator Functions:
|
|
250
|
+
- `wb_gdp_deflate`: Uses GDP deflators.
|
|
251
|
+
- `wb_gdp_linked_deflate`: Uses the World Bank’s linked GDP deflator series.
|
|
252
|
+
- `wb_cpi_deflate`: Uses Consumer Price Index data.
|
|
253
|
+
|
|
254
|
+
Exchange Function:
|
|
255
|
+
- `wb_exchange`: Uses yearly average exchange rates.
|
|
256
|
+
|
|
257
|
+
Notes:
|
|
258
|
+
- The linked GDP deflator series counters breaks in series over time due to changes in base years or methodologies.
|
|
259
|
+
- Exchange rates are based on IMF International Financial Statistics data
|
|
260
|
+
|
|
261
|
+
### OECD Development Assistance Committee
|
|
262
|
+
|
|
263
|
+
Deflator Function:
|
|
264
|
+
- `oecd_dac_deflate`: Uses the DAC’s own deflator series.
|
|
265
|
+
|
|
266
|
+
Exchange Function:
|
|
267
|
+
- `oecd_dac_exchange`: Uses exchange rates used and published by the DAC.
|
|
268
|
+
|
|
269
|
+
### Sources
|
|
270
|
+
This package relies on data from the following sources:
|
|
271
|
+
- OECD DAC: https://www.oecd.org/dac/
|
|
272
|
+
- IMF World Economic Outlook: https://www.imf.org/en/Publications/WEO
|
|
273
|
+
- World Bank DataBank: https://databank.worldbank.org/home.aspx
|
|
274
|
+
|
|
275
|
+
This data is provided based on the terms and conditions set by the
|
|
276
|
+
original sources.
|
|
277
|
+
|
|
278
|
+
## Handling Missing Data
|
|
279
|
+
|
|
280
|
+
Pydeflate relies on data from external sources. If there are missing values in the deflator or exchange rate data for certain countries or years, pydeflate will flag this in the output DataFrame. Ensure that your data aligns with the available data from the selected source.
|
|
281
|
+
|
|
282
|
+
## Updating Underlying Data
|
|
283
|
+
|
|
284
|
+
Pydeflate periodically updates its underlying data from the World Bank, IMF, and OECD. If the data on your system is older than 50 days, pydeflate will display a warning upon import.
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
pydeflate/.pydeflate_data/README.md,sha256=atNtUL9dD8G184YSd6juFib8TgEQBcSLogiz99APPVs,25
|
|
2
|
+
pydeflate/__init__.py,sha256=i6sHAqjE6qoEc4l1Gy1w7PN6BVhR61RqVBpn3BSDrSc,975
|
|
3
|
+
pydeflate/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
pydeflate/core/api.py,sha256=1MzGu8xLgbUWksy_IMBuK_HgyPLSFawMgLLJsNKJSok,14351
|
|
5
|
+
pydeflate/core/deflator.py,sha256=Ax3dmOF3tYRZnkIfFvMMo3SOLgAJHkXSmA-OtIUZkp0,5932
|
|
6
|
+
pydeflate/core/exchange.py,sha256=chPqAo_MjEcwb49GVBlTu0s8ZhFchnjtZVCYsvVp_d0,8532
|
|
7
|
+
pydeflate/core/source.py,sha256=tIN9fAPhhE-6C5Ye608qTMQblm3yva7u5p1nJwepXm4,1724
|
|
8
|
+
pydeflate/deflate/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
pydeflate/deflate/deflators.py,sha256=hyLFoX46BAn5m8gTLjw-TFv3x3W6CTQmvmUvq5a_cio,7768
|
|
10
|
+
pydeflate/deflate/legacy_deflate.py,sha256=9pfqsi5KeWgP1yhXeI6K7bAjUeFY-fmRxrpDB7Zu0zo,3900
|
|
11
|
+
pydeflate/exchange/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
pydeflate/exchange/exchangers.py,sha256=DWHvfH7FivgPV25R3dvBuMHWA9bxlvTDQPzg062AMN0,5384
|
|
13
|
+
pydeflate/pydeflate_config.py,sha256=5s4SLJf5is5XcUgJHDRx4f27pPiaVh0H2BL8w9QjW0k,1097
|
|
14
|
+
pydeflate/settings/emu.json,sha256=BIvbiMUeHUtCESR3sMcBNrS028yp2YraCJdhDJGvAAo,133
|
|
15
|
+
pydeflate/settings/oecd_codes.json,sha256=jAKI1EgQP4rttjoG3Z-44r1tUJrIEzPCZF5V2aboQhE,911
|
|
16
|
+
pydeflate/sources/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
|
+
pydeflate/sources/common.py,sha256=Ot0AKeQx2M7juID8qvFFpiPcCJNR9UzrpYLonTcHyCQ,9630
|
|
18
|
+
pydeflate/sources/dac.py,sha256=ngFiApGZ_tIQg74ogGVTIbGUA0efnF1SYwfUuqGofOQ,3791
|
|
19
|
+
pydeflate/sources/imf.py,sha256=10vc8xhNJvANb7RDD1WFn9oaZ8g53yUV5LxCQCz6ImM,6337
|
|
20
|
+
pydeflate/sources/world_bank.py,sha256=YxQgUUDMXlPArIfvYJTjNIAU5_I_OeUYDzc7NtTq9EA,6288
|
|
21
|
+
pydeflate/utils.py,sha256=sfocGBQook3S7gvutCFovknyCv0cV37MHPJB-EMIumQ,2321
|
|
22
|
+
pydeflate-2.0.0.dist-info/LICENSE,sha256=q5tm9mQxwSbV5Ivvjxs7MMqBgan6DM8I4r4irPvmqZM,1075
|
|
23
|
+
pydeflate-2.0.0.dist-info/METADATA,sha256=MxV_NdB1JYX3or3k1EoMS5nDuY2dItSNHgZVCJIrq_o,12582
|
|
24
|
+
pydeflate-2.0.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
25
|
+
pydeflate-2.0.0.dist-info/RECORD,,
|
pydeflate/deflate/deflate.py
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
import pandas as pd
|
|
2
|
-
|
|
3
|
-
from pydeflate.deflate.deflator import Deflator
|
|
4
|
-
from pydeflate.get_data.exchange_data import (
|
|
5
|
-
ExchangeIMF,
|
|
6
|
-
ExchangeOECD,
|
|
7
|
-
ExchangeWorldBank,
|
|
8
|
-
)
|
|
9
|
-
from pydeflate.get_data.imf_data import IMF
|
|
10
|
-
from pydeflate.get_data.oecd_data import OECD
|
|
11
|
-
from pydeflate.get_data.wb_data import WorldBank
|
|
12
|
-
from pydeflate.pydeflate_config import logger
|
|
13
|
-
from pydeflate.utils import check_year_as_number, oecd_codes, to_iso3
|
|
14
|
-
|
|
15
|
-
ValidDeflatorSources: dict = {
|
|
16
|
-
"oecd_dac": "dac_deflator",
|
|
17
|
-
"dac": "dac_deflator",
|
|
18
|
-
"wb": "world_bank",
|
|
19
|
-
"world_bank": "world_bank",
|
|
20
|
-
"imf": "imf",
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
ValidDeflatorMethods: dict = {
|
|
24
|
-
"oecd_dac": ["dac_deflator"],
|
|
25
|
-
"world_bank": ["gdp", "gdp_linked", "cpi"],
|
|
26
|
-
"imf": ["gdp", "pcpi", "pcpie"],
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
ValidExchangeSources: dict = {
|
|
30
|
-
"oecd_dac": "oecd_dac",
|
|
31
|
-
"dac": "oecd_dac",
|
|
32
|
-
"wb": "world_bank",
|
|
33
|
-
"world_bank": "world_bank",
|
|
34
|
-
"imf": "world_bank",
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
ValidExchangeMethods: dict = {
|
|
38
|
-
"oecd_dac": ["implied"],
|
|
39
|
-
"world_bank": ["yearly_average", "effective_exchange"],
|
|
40
|
-
"imf": ["implied"],
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
ExchangeSources: dict = {
|
|
44
|
-
"oecd_dac": {
|
|
45
|
-
"implied": ExchangeOECD(method="implied"),
|
|
46
|
-
},
|
|
47
|
-
"world_bank": {
|
|
48
|
-
"yearly_average": ExchangeWorldBank(method="yearly_average"),
|
|
49
|
-
"effective_exchange": ExchangeWorldBank(method="effective_exchange"),
|
|
50
|
-
},
|
|
51
|
-
"imf": {
|
|
52
|
-
"implied": ExchangeIMF(method="implied"),
|
|
53
|
-
},
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
DeflatorSources: dict = {
|
|
57
|
-
"oecd_dac": OECD,
|
|
58
|
-
"world_bank": WorldBank,
|
|
59
|
-
"imf": IMF,
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def _validate_deflator_source(deflator_source) -> str:
|
|
64
|
-
if deflator_source not in ValidDeflatorSources:
|
|
65
|
-
raise ValueError(f'"{deflator_source=}" not valid')
|
|
66
|
-
return deflator_source
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def _validate_deflator_method(deflator_source, deflator_method) -> str:
|
|
70
|
-
# Check if the method specified is part of the available deflators methods
|
|
71
|
-
if (
|
|
72
|
-
deflator_method is not None
|
|
73
|
-
and deflator_method not in ValidDeflatorMethods[deflator_source]
|
|
74
|
-
):
|
|
75
|
-
raise ValueError(f'"{deflator_method=}" not valid')
|
|
76
|
-
|
|
77
|
-
# Default to the first method in the list of valid methods for the source
|
|
78
|
-
if deflator_method is None:
|
|
79
|
-
deflator_method = ValidDeflatorMethods[deflator_source][0]
|
|
80
|
-
|
|
81
|
-
return deflator_method
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
def _validate_exchange_source(deflator_method, exchange_source) -> str:
|
|
85
|
-
# Check if the source specified is part of the available exchange source
|
|
86
|
-
if exchange_source is not None and exchange_source not in ValidExchangeSources:
|
|
87
|
-
raise ValueError(f'"{exchange_source=}" not valid')
|
|
88
|
-
|
|
89
|
-
# Default to the first source in the list of valid sources
|
|
90
|
-
if exchange_source is None:
|
|
91
|
-
exchange_source = ValidExchangeMethods[deflator_method][0]
|
|
92
|
-
|
|
93
|
-
return exchange_source
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
def _validate_exchange_method(exchange_source, exchange_method) -> str:
|
|
97
|
-
# Check if the method specified is part of the available exchange methods
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
exchange_method is not None
|
|
101
|
-
and exchange_method not in ValidExchangeMethods[exchange_source]
|
|
102
|
-
):
|
|
103
|
-
raise ValueError(f'"{exchange_method=}" not valid')
|
|
104
|
-
|
|
105
|
-
# Default to the first method in the list of valid methods for the source
|
|
106
|
-
if exchange_method is None:
|
|
107
|
-
exchange_method = ValidExchangeMethods[exchange_source][0]
|
|
108
|
-
|
|
109
|
-
return exchange_method
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def _create_id_col(df: pd.DataFrame, id_type: str, id_column: str) -> pd.DataFrame:
|
|
113
|
-
"""Create an id column.
|
|
114
|
-
|
|
115
|
-
By default, if a country does not have a DAC deflator, the DAC, Total deflator is used.
|
|
116
|
-
"""
|
|
117
|
-
|
|
118
|
-
if id_type == "DAC":
|
|
119
|
-
df["id_"] = df[id_column].map(oecd_codes()).fillna("DAC")
|
|
120
|
-
else:
|
|
121
|
-
df = df.pipe(
|
|
122
|
-
to_iso3, codes_col=id_column, target_col="id_", src_classification=id_type
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
return df
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
def deflate(
|
|
129
|
-
df: pd.DataFrame,
|
|
130
|
-
base_year: int,
|
|
131
|
-
deflator_source: str = None,
|
|
132
|
-
deflator_method: str | None = None,
|
|
133
|
-
exchange_source: str | None = None,
|
|
134
|
-
exchange_method: str | None = None,
|
|
135
|
-
source_currency: str = "USA",
|
|
136
|
-
target_currency: str = "USA",
|
|
137
|
-
id_column: str = "iso_code",
|
|
138
|
-
id_type: str = "ISO3",
|
|
139
|
-
date_column: str = "date",
|
|
140
|
-
source_column: str = "value",
|
|
141
|
-
target_column: str = "value",
|
|
142
|
-
to_current: bool = False,
|
|
143
|
-
method: str | None = None,
|
|
144
|
-
source: str | None = None,
|
|
145
|
-
iso_column: str | None = None,
|
|
146
|
-
source_col: str | None = None,
|
|
147
|
-
target_col: str | None = None,
|
|
148
|
-
) -> pd.DataFrame:
|
|
149
|
-
"""Deflate amounts to a given currency - base year combination.
|
|
150
|
-
|
|
151
|
-
Takes a DataFrame containing flows data and returns a DataFrame containing
|
|
152
|
-
the deflated amounts. It can also convert from constant to current by specifying
|
|
153
|
-
to_current as 'True'.
|
|
154
|
-
|
|
155
|
-
Args:
|
|
156
|
-
df: the DataFrame containing the flows column to be deflated. If multiple
|
|
157
|
-
columns need to be deflated, the function needs to be called multiple times.
|
|
158
|
-
base_year: If converting from current to constant, the target base year for the
|
|
159
|
-
constant figures. If converting from constant, the base year of the data.
|
|
160
|
-
deflator_source:{‘oecd_dac’, 'wb', 'imf'}
|
|
161
|
-
The source of the data used to build the deflators. The value (and
|
|
162
|
-
completeness) of the price deflators may change based on the source.
|
|
163
|
-
Additionally, the OECD DAC data is only available for DAC donors.
|
|
164
|
-
deflator_method:{'gdp', 'gdp_linked', 'cpi', 'pcpi', 'pcpie', None}
|
|
165
|
-
The method used to calculate the price deflator:
|
|
166
|
-
|
|
167
|
-
For World Bank (source == 'wb'), default is "gdp":
|
|
168
|
-
|
|
169
|
-
•'gdp': using GDP deflators.
|
|
170
|
-
|
|
171
|
-
•'gdp_linked': a GDP deflator series which has been linked to
|
|
172
|
-
produce a consistent time series to counteract breaks in series
|
|
173
|
-
over time due to changes in base years, sources or methodologies.
|
|
174
|
-
|
|
175
|
-
•'cpi': using Consumer Price Index data
|
|
176
|
-
|
|
177
|
-
For IMF (source == 'imf'), default is "gdp":
|
|
178
|
-
|
|
179
|
-
•'pcpi': Consumer Price Index data
|
|
180
|
-
|
|
181
|
-
•'pcpie': end-period Consumer Price Index (e.g. for December each year).
|
|
182
|
-
|
|
183
|
-
For OECD DAC (source == 'oecd_dac'), default is "dac_deflator":
|
|
184
|
-
•'dac_deflator': using the OECD DAC deflator
|
|
185
|
-
|
|
186
|
-
exchange_source: The source of the exchange rates. If None, the exchange rates
|
|
187
|
-
of the deflator_source will be used.
|
|
188
|
-
|
|
189
|
-
exchange_method: The method used to calculate the exchange rates. If None, the
|
|
190
|
-
default exchange method of the exchange_source will be used.
|
|
191
|
-
|
|
192
|
-
source_currency: The iso3 code of the source currency. Note that deflators for EU countries
|
|
193
|
-
are only in Euros from the year in which the Euro was adopted. To produce
|
|
194
|
-
deflators only in euros, use 'emu'.
|
|
195
|
-
|
|
196
|
-
target_currency :The iso3 code of the deflated amounts. It can be the same as the source
|
|
197
|
-
currency. In cases where they are different, the exchange rate will be
|
|
198
|
-
applied. To produce deflators only in euros, use 'emu'.
|
|
199
|
-
|
|
200
|
-
id_column: The column containing the id codes (iso3 codes, for example) of the data's
|
|
201
|
-
currency.
|
|
202
|
-
|
|
203
|
-
id_type:The classification type for the id_column. By default, ISO3 but others are possible.
|
|
204
|
-
Any options used in the Country Converter package are valid. For the OECD DAC
|
|
205
|
-
classification, use 'DAC'.
|
|
206
|
-
|
|
207
|
-
date_column:The column containing the date values. The column can contain years (int)
|
|
208
|
-
or datetime objects.
|
|
209
|
-
|
|
210
|
-
source_column:The column containing the data to be deflated.
|
|
211
|
-
|
|
212
|
-
target_column: Column where the deflated data will be stored. It can be the same as the
|
|
213
|
-
source column if a copy of the original isn't needed.
|
|
214
|
-
|
|
215
|
-
to_current: If True, amounts will be treated as in constant prices and converted to
|
|
216
|
-
current prices.
|
|
217
|
-
|
|
218
|
-
iso_column:Provided for backwards compatibility. An alias for id_column
|
|
219
|
-
source: Provided for backwards compatibility. An alias for deflator_source
|
|
220
|
-
method: Provided for backwards compatibility. An alias for deflator_method
|
|
221
|
-
source_col: Provided for backwards compatibility. An alias for source_column
|
|
222
|
-
target_col: Provided for backwards compatibility. An alias for target_column
|
|
223
|
-
|
|
224
|
-
Returns:
|
|
225
|
-
A pandas DataFrame containing the deflated data. Years for which there
|
|
226
|
-
are no deflators will be returned as null values.
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
"""
|
|
230
|
-
|
|
231
|
-
# ------------------ Backwards compatibility ------------------
|
|
232
|
-
|
|
233
|
-
# Backwards compatibility: if the iso_column parameter is used, reassign it
|
|
234
|
-
if iso_column is not None:
|
|
235
|
-
id_column = iso_column
|
|
236
|
-
logger.warning("iso_column is deprecated. Use id_column instead.")
|
|
237
|
-
|
|
238
|
-
# Backwards compatibility: if the source parameter is used, reassign it
|
|
239
|
-
if deflator_source is None and source is not None:
|
|
240
|
-
deflator_source = source
|
|
241
|
-
logger.warning("source is deprecated. Use deflator_source instead.")
|
|
242
|
-
|
|
243
|
-
# Backwards compatibility: if the method parameter is used, reassign it
|
|
244
|
-
if method is not None:
|
|
245
|
-
deflator_method = method
|
|
246
|
-
logger.warning("method is deprecated. Use deflator_method instead.")
|
|
247
|
-
|
|
248
|
-
# Backwards compatibility: if the source_col parameter is used, reassign it
|
|
249
|
-
if source_col is not None:
|
|
250
|
-
source_column = source_col
|
|
251
|
-
logger.warning("source_col is deprecated. Use source_column instead.")
|
|
252
|
-
|
|
253
|
-
# Backwards compatibility: if the target_col parameter is used, reassign it
|
|
254
|
-
if target_col is not None:
|
|
255
|
-
target_column = target_col
|
|
256
|
-
logger.warning("target_col is deprecated. Use target_column instead.")
|
|
257
|
-
|
|
258
|
-
# -------------------------- Validation -----------------------------
|
|
259
|
-
deflator_source = _validate_deflator_source(deflator_source=deflator_source)
|
|
260
|
-
|
|
261
|
-
deflator_method = _validate_deflator_method(
|
|
262
|
-
deflator_source=deflator_source, deflator_method=deflator_method
|
|
263
|
-
)
|
|
264
|
-
|
|
265
|
-
exchange_source = _validate_exchange_source(
|
|
266
|
-
deflator_method=exchange_method, exchange_source=exchange_source
|
|
267
|
-
)
|
|
268
|
-
|
|
269
|
-
exchange_method = _validate_exchange_method(
|
|
270
|
-
exchange_source=exchange_source, exchange_method=exchange_method
|
|
271
|
-
)
|
|
272
|
-
|
|
273
|
-
# ----------------------------- Set up ---------------------------
|
|
274
|
-
|
|
275
|
-
# copy the dataframe to avoid modifying the original
|
|
276
|
-
df = df.copy(deep=True)
|
|
277
|
-
|
|
278
|
-
# Keep track of original columns to return data in the same order.
|
|
279
|
-
if target_column not in df.columns:
|
|
280
|
-
cols = [*df.columns, target_column]
|
|
281
|
-
else:
|
|
282
|
-
cols = df.columns
|
|
283
|
-
|
|
284
|
-
# check if date format matches
|
|
285
|
-
df, year_as_number = check_year_as_number(df=df, date_column=date_column)
|
|
286
|
-
|
|
287
|
-
# create id_column
|
|
288
|
-
df = _create_id_col(df=df, id_column=id_column, id_type=id_type)
|
|
289
|
-
|
|
290
|
-
# ----------------------------- Load deflator data ---------------------------
|
|
291
|
-
deflator = (
|
|
292
|
-
Deflator(
|
|
293
|
-
base_year=base_year,
|
|
294
|
-
exchange_obj=ExchangeSources[exchange_source][exchange_method],
|
|
295
|
-
deflator_obj=DeflatorSources[deflator_source](),
|
|
296
|
-
deflator_method=deflator_method,
|
|
297
|
-
source_currency=source_currency,
|
|
298
|
-
target_currency=target_currency,
|
|
299
|
-
to_current=to_current,
|
|
300
|
-
)
|
|
301
|
-
.get_deflator()
|
|
302
|
-
.rename(columns={"iso_code": "id_", "year": date_column})
|
|
303
|
-
)
|
|
304
|
-
|
|
305
|
-
# ----------------------------- Deflate ---------------------------
|
|
306
|
-
# Merge the original data with the deflator DataFrame
|
|
307
|
-
df = df.merge(
|
|
308
|
-
deflator,
|
|
309
|
-
on=["id_", date_column],
|
|
310
|
-
how="left",
|
|
311
|
-
suffixes=("", "_"),
|
|
312
|
-
)
|
|
313
|
-
|
|
314
|
-
# If the reverse option is used, multiply the data. Else divide.
|
|
315
|
-
df[target_column] = df[source_column] / (df.deflator / 100)
|
|
316
|
-
|
|
317
|
-
# Keep only the columns present in the original DataFrame (including order)
|
|
318
|
-
df = df.filter(cols, axis=1)
|
|
319
|
-
|
|
320
|
-
# If the year is passed as a number, convert it back to a number.
|
|
321
|
-
if year_as_number:
|
|
322
|
-
df[date_column] = df[date_column].dt.year
|
|
323
|
-
|
|
324
|
-
return df
|