SDMX: Statistical Data and Metadata eXchange

2.26.0 · active · verified Fri Apr 17

sdmx1 is a Python library for consuming and working with Statistical Data and Metadata eXchange (SDMX) web services and files. It supports various SDMX versions and data formats, allowing users to query, download, and parse statistical data from official sources like Eurostat, IMF, and OECD. The library is actively maintained with frequent minor releases, currently at version 2.26.0, and requires Python >=3.10.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to connect to an SDMX agency (e.g., IMF) using `sdmx.Request`, list available dataflows, and print basic information. It includes error handling for common API issues. The example for fetching specific data is commented out as it requires specific knowledge of an agency's data structure.

import sdmx
import os

# Example agency ID; replace with a real one like 'ESTAT' (Eurostat), 'IMF', 'OECD'
# Some agencies may require specific authentication or have strict rate limits.
agency_id = os.environ.get('SDMX_AGENCY_ID', 'IMF') # Using IMF as a common example

try:
    # Create a Request object for the specified agency
    req = sdmx.Request(agency_id)

    print(f"Attempting to connect to SDMX agency: {agency_id}")

    # Fetch available dataflows
    # This performs an HTTP GET request to the agency's API endpoint
    dataflows = req.dataflow()

    print(f"Successfully retrieved dataflows from {agency_id}.")
    print(f"First 3 dataflows from {agency_id} (ID: Name):")
    if dataflows.data.dataflow:
        for i, flow in enumerate(dataflows.data.dataflow[:3]):
            print(f"  - {flow.id}: {flow.name.get('en', 'No English name')}")
    else:
        print("  No dataflows found.")

    # To fetch actual data, you would then use a specific dataflow ID and keys.
    # Example (commented out, as specific dataflow IDs and keys vary greatly):
    # # For IMF, using International Financial Statistics (IFS) dataflow, if available
    # if agency_id == 'IMF':
    #     print("\nAttempting to fetch data from IMF (example).")
    #     data = req.get_data(
    #         resource_id='IFS',
    #         key={'REF_AREA': ['US', 'CN'], 'INDICATOR': ['LP_CPI_IX']},
    #         params={'startPeriod': '2020', 'endPeriod': '2022'}
    #     )
    #     print(f"Fetched {len(data.series)} series from IFS.")

except sdmx.api.APIError as e:
    print(f"Error connecting to or fetching data from {agency_id}: {e}")
    print("Possible causes: invalid agency ID, network issues, API rate limits, or specific API endpoint errors.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →