bblocks.import_tools.sdr

Attributes

BASE_URL

MAIN_PAGE_URL

EXCHANGE_URL

Classes

SDR

An object to import SDR data

Functions

create_tsv_link(→ str)

Generate a TSV download link for a specific date

get_response(→ requests.models.Response)

Request a response from a url

parse_sdr_links(→ dict)

Function to parse SDR tables. returns a dictionary of dates and links

get_latest_date(→ str)

Get the latest available date

clean_df(→ pandas.DataFrame)

Clean the SDR dataframe

read_tsv(→ pandas.DataFrame)

Read a tsv file from a url and return a dataframe

get_data(→ pandas.DataFrame)

Get the SDR _data for a specific date

format_date(→ str)

Format a date string containing year and month, and adds the last day for that month-year

check_if_not_downloaded(→ bool)

Checks if _data is already downloaded for an indicator and area grouping

__get_rate(→ float)

Returns currency value from SDR exchange rate table

__get_exchange_date(→ str)

Returns date of exchange rate table

parse_exchange(→ tuple[str, float])

Parse the exchange rate response

get_latest_exchange_rate(→ float | dict)

Get the latest exchange rate for a specific currency

Module Contents

bblocks.import_tools.sdr.BASE_URL = 'https://www.imf.org/external/np/fin/tad/'
bblocks.import_tools.sdr.MAIN_PAGE_URL = 'https://www.imf.org/external/np/fin/tad/extsdr1.aspx'
bblocks.import_tools.sdr.EXCHANGE_URL = 'https://www.imf.org/external/np/fin/data/rms_sdrv.aspx'

Generate a TSV download link for a specific date

bblocks.import_tools.sdr.get_response(url: str) requests.models.Response

Request a response from a url

Function to parse SDR tables. returns a dictionary of dates and links

bblocks.import_tools.sdr.get_latest_date() str

Get the latest available date

bblocks.import_tools.sdr.clean_df(df: pandas.DataFrame, date: str) pandas.DataFrame

Clean the SDR dataframe

bblocks.import_tools.sdr.read_tsv(url: str) pandas.DataFrame

Read a tsv file from a url and return a dataframe

bblocks.import_tools.sdr.get_data(date: str) pandas.DataFrame

Get the SDR _data for a specific date

bblocks.import_tools.sdr.format_date(date: list | tuple) str

Format a date string containing year and month, and adds the last day for that month-year

bblocks.import_tools.sdr.check_if_not_downloaded(date: str) bool

Checks if _data is already downloaded for an indicator and area grouping :returns: True if _data is not downloaded, False if _data is downloaded

bblocks.import_tools.sdr.__get_rate(table: bs4.BeautifulSoup, currency: str) float

Returns currency value from SDR exchange rate table

bblocks.import_tools.sdr.__get_exchange_date(table: bs4.BeautifulSoup) str

Returns date of exchange rate table

bblocks.import_tools.sdr.parse_exchange(response: bytes, currency: str) tuple[str, float]

Parse the exchange rate response

bblocks.import_tools.sdr.get_latest_exchange_rate(currency: str = 'USD', only_value: bool = False) float | dict

Get the latest exchange rate for a specific currency

Parameters:
  • currency – Currency to get exchange rate for. Default is USD. Choose from ‘USD’ or ‘SDR’

  • only_value – If False, a dictionary containing the date and exchange rate is returned. If True, only the exchange rate is returned. Default is False.

Returns:

A dictionary containing the date and exchange rate, or only the exchange rate

class bblocks.import_tools.sdr.SDR

Bases: bblocks.import_tools.common.ImportData

An object to import SDR data

An object to help extract and store the latest Special Drawing Rights (SDR) data from the IMF website: https://www.imf.org/external/np/fin/tad/extsdr1.aspx

In order to use, create an instance of this class. Then, call the load_data method to load SDR _data for a specific date. If no date is provided, the latest date will be found and loaded. Call latest_date to get the latest date available. If the data for a specific date has never been downloaded, it will be downloaded. If it has been downloaded, it will be loaded from disk. You can force an update by calling update_data if you want to refresh the data stored on disk and in the object. Call get_data to get the data as a DataFrame.

__latest_date: str = None
__repr__()
latest_date()

Return the latest date of the _data

load_data(date: str | tuple = 'latest') bblocks.import_tools.common.ImportData

Load the SDR _data for a specific date

Returns:

the same object to allow chaining

update_data(reload: bool) bblocks.import_tools.common.ImportData

Update the data stored on disk and in the object

Returns:

the same object to allow chaining

get_data(date: str = None, indicator: str = None) pandas.DataFrame

Get SDR _data as a DataFrame

Parameters:
  • date – the date of the _data to be retrieved. If no date is provided, _data for all dates will be retrieved If ‘latest’ is provided, all the _data will be returned.

  • indicator – the indicator to be retrieved, choose from [‘holdings’, ‘allocations]. If no indicator is provided, all the indicators will be returned.

Returns:

a DataFrame containing the SDR _data