Search code examples
pandassdmx

Get currency of today from ECB - pandassdmx?


I need to get the latest currency rates from ECB for USD/EUR and CHF/EUR. What is the recommended method?

Version 1: Tried pandasdmx but do not know the most efficient way to extract the value:

ecb = sdmx.Request("ECB")
parameters = {
    "startPeriod": "2021-12-29",
    "endPeriod": "2021-12-29",
}
data_response = ecb.data(
    resource_id="EXR",
    key={"CURRENCY": ["CHF", "USD"]},
    params=parameters,
)

How do I parse the data_response to get the values for USD and CHF?

Version 2: An alternative way I found was to access

request_url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"

Parsing the XML is easy but the first version seems to be the more updated one.

Any thoughts?


Solution

  • You can do

    data = data_response.to_pandas()
    data
    

    to get the results as a Pandas Series:

    FREQ  CURRENCY  CURRENCY_DENOM  EXR_TYPE  EXR_SUFFIX  TIME_PERIOD
    D     CHF       EUR             SP00      A           2021-12-29     1.0380
          USD       EUR             SP00      A           2021-12-29     1.1303
    Name: value, dtype: float64
    

    The values of the data Series are the two exchange rates you are after. All the previous fields are part of a multi-index so to get a CHF you can use

    data[('D', 'CHF', 'EUR', 'SP00', 'A', '2021-12-29')]
    

    this will return

    1.038
    

    or you can use .iloc

    data.iloc[0]
    

    for CHF, data.iloc[1] for USD