bblocks.import_tools.imf_weo
Tools to import IMF World Economic Outlook data.
Attributes
Classes
Helper class to parse WEO data |
|
An object to extract WEO data from the IMF website using SDMX |
Functions
|
Generate the url for the SDMX query |
|
Parse the response from the SDMX query |
|
retrieve the href for the SDMX file |
|
Generates the latest expected version based on the current date |
|
Roll back version to the previous version |
|
Downloads latest data or data for specified version |
Module Contents
- bblocks.import_tools.imf_weo.BASE_URL = 'https://www.imf.org/'
- bblocks.import_tools.imf_weo.COLUMN_MAPPER
- bblocks.import_tools.imf_weo._smdx_query_url(version: tuple[int, int]) str
Generate the url for the SDMX query
- bblocks.import_tools.imf_weo._parse_sdmx_query_response(content: requests.Response.content) str
Parse the response from the SDMX query
- bblocks.import_tools.imf_weo.get_sdmx_href(version: tuple[int, int]) str | None
retrieve the href for the SDMX file
- Parameters:
version (tuple[int, int]) – the version of the WEO data as a tuple of (year, version)
- class bblocks.import_tools.imf_weo.Parser(folder: zipfile.ZipFile)
Helper class to parse WEO data
This class parses the data and schema files into a clean dataframe to be used in the main WEO class
- Parameters:
folder – the folder containing the data and schema files
- folder: zipfile.ZipFile
- data_file: xml.etree.ElementTree = None
- schema_file: xml.etree.ElementTree = None
- data: pandas.DataFrame | None = None
- get_files() None
Get the root of files
- _extract_data() None
extract data from data file
- _convert_series_codes(series: pandas.Series, lookup_value: str) pandas.Series
Converts a series of codes to the corresponding values in the schema file
- _clean_data() None
Clean and format the dataframe
- parse_data() None
Parse the data to a dataframe
- get_data() pandas.DataFrame
Get the data. If the data is not already parsed, parse it first
- bblocks.import_tools.imf_weo.gen_latest_version() tuple[int, int]
Generates the latest expected version based on the current date
- bblocks.import_tools.imf_weo.roll_back_version(version: tuple[int, int]) tuple[int, int]
Roll back version to the previous version
- bblocks.import_tools.imf_weo.extract_data(version) pandas.DataFrame | None
Downloads latest data or data for specified version
- Parameters:
version (tuple[int, int]) – version to download
- class bblocks.import_tools.imf_weo.WEO
Bases:
bblocks.import_tools.common.ImportDataAn object to extract WEO data from the IMF website using SDMX To use, create an instance of the class setting the version to the desired version, or leave blank to get the latest version. Call the load_data method to load the data to the object. If the data is already downloaded, it will load from disk, otherwise it will download the data. To force an update of the data, call the update_data method. To retrieve the data, call the get_data method.
- version
tuple of (year, release) or “latest”. Default is “latest”
- version: str | tuple[int, int] = 'latest'
- __post_init__()
check that version is valid
- property _path
Generate path based on version
- _download_data() None
Downloads latest data or data for specified version if not already available in disk
If version is “latest” it will generate the expected version based on the current date and try download the data. If it is not available for the expected version, it will roll back the version and try again. If no data is found, it will raise an error.
If version is a tuple of (year, release), it will try download the data for that version
- load_data(indicators: str | list = 'all') bblocks.import_tools.common.ImportData
Load data to object
When called this method will load WEO indicators to the object. If the data is not downloaded, it will download the data. If the data is already downloaded, it will load the data from disk. If the version is set to ‘latest’, it will find the latest version and download the data.
- Parameters:
indicators – The indicators to load. If ‘all’ is passed, all indicators will be loaded. Default is ‘all’
- Returns:
same object with data loaded
- update_data(reload_data: bool = True) bblocks.import_tools.common.ImportData
Update the data
When called it will update the data used to load the indicators. If reload_data is True, it will reload the indicators to the object.
- Parameters:
reload_data – If True, reload the indicators after updating the data. Default is True.
- Returns:
same object with updated data to allow chaining
- available_indicators() dict[str, str]
Returns a DataFrame of available indicators to load to the object
- get_old_format_data() pandas.DataFrame
This function returns the data in the old format that weo-reader returns
NOTE: This will return all the data in the object in the old format, regardless of the indicators loaded Not all columns that existed in weo-reader are returned as they don’t exist in the sdmx data files. However, this should not cause issues are they are metadata columns not used in analysis.