This page walks through an example pandaSDMX workflow, providing explanations of some SDMX concepts along the way. See also Resources, HOWTOs for miscellaneous tasks, and follow links to the Glossary where some terms are explained.

SDMX workflow

Working with statistical data often includes some or all of the following steps. pandaSDMX builds on SDMX features to make the steps straightforward:

  1. Choose a data provider.

    pandaSDMX provides a built-in list of Data sources.

  2. Investigate what data is available.

    Using pandaSDMX, download the catalogue of data flows available from the data provider and select a data flow for further inspection.

  3. Understand what form the data comes in.

    Using pandaSDMX, download structure and metadata on the selected data flow and the data it contains, including the data structure definition, concepts, codelists and content constraints.

  4. Decide what data is required.

    Using pandaSDMX, analyze the structural metadata, by directly inspecting objects or converting them to pandas types.

  5. Download the actual data.

    Using pandaSDMX, specify the needed portions of the data from the data flow by constructing a selection (‘key’) of series and a period/time range. Then, retrieve the data using Request.get().

  6. Analyze or manipulate the data.

    Convert to pandas types using pandasmdx.message.Message.to_pandas() or, equivalently, pandasdmx.api.to_pandas() and use the result in further Python code.

Choose and connect to an SDMX web service

First, we instantiate a pandasdmx.Request object, using the string ID of a data source supported by pandaSDMX:

In [1]: import pandasdmx as sdmx

In [2]: ecb = sdmx.Request('ECB')

The object ecb is now ready to make multiple data and metadata queries to the European Central Bank’s web service. To send requests to multiple web services, we could instantiate multiple Requests.

Configure the HTTP connection

pandaSDMX builds on the widely-used requests Python HTTP library. To pre-configure all queries made by a Request, we can pass any of the keyword arguments recognized by requests.request(). For example, a proxy server can be specified:

In [3]: ecb_via_proxy = sdmx.Request(
   ...:     'ECB',
   ...:     proxies={'http': ''}
   ...: )

The session attribute is a familiar requests.Session object that can be used to inspect and modify configuration between queries:

In [4]: ecb_via_proxy.session.proxies
Out[4]: {'http': ''}

For convenience, timeout stores the timeout in seconds for HTTP requests, and is passed automatically for all queries.

Cache HTTP responses and parsed objects

New in version 0.3.0.

If requests_cache is installed, it is used automatically by Session. To configure it, we can pass any of the arguments accepted by requests_cache.core.CachedSession when creating a Request. For example, to force requests_cache to use SQLite to store cached data with the fast_save option, and expire cache entries after 10 minutes:

In [5]: ecb_with_cache = sdmx.Request(
   ...:     'ECB',
   ...:     backend='sqlite',
   ...:     fast_save=True,
   ...:     expire_after=600,
   ...: )

In addition, Request provides an optional, simple dict-based cache for retrieved and parsed Message instances, where the cache key is the constructed query URL. This cache is disabled by default; to activate it, supply use_cache=True to the constructor.

Using custom sessions

New in version 1.0.0.

The Request constructor takes an optional keyword argument session. For instance, a requests.Session with pre-mounted adapters or patched by an alternative caching library such as CacheControl can be passed:

>>> awesome_ecb_req = Request('ECB', session=my_awesome_session)

Obtain and explore metadata

This section illustrates how to download and explore metadata. Suppose we are looking for time-series on exchange rates, and we know that the European Central Bank provides a relevant data flow.

We could search the Internet for the dataflow ID or browse the ECB’s website. However, we can also use pandaSDMX to retrieve metadata and get a complete overview of the dataflows the ECB provides.

Get information about the source’s data flows

We use pandaSDMX to download the definitions for all data flows available from our chosen source. We could call Request.get() with [resource_type=]'dataflow' as the first argument, but can also use a shorter alias:

In [6]: flow_msg = ecb.dataflow()

The query returns a Message instance. We can also see the URL that was queried and the response headers by accessing the Message.response attribute:

In [7]: flow_msg.response.url
Out[7]: 'http://sdw-wsrest.ecb.int/service/dataflow/ECB/latest'

In [8]: flow_msg.response.headers
Out[8]: {'Server': 'myracloud', 'Date': 'Fri, 15 May 2020 10:16:49 GMT', 'Content-Type': 'application/vnd.sdmx.structure+xml; version=2.1', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Vary': 'accept-encoding, accept, origin', 'Cache-Control': 'no-cache, no-store, max-age=0', 'ETag': '"myra-6438c272"'}

All the content of the response—SDMX data and metadata objects—has been parsed and is accessible from flow_msg. Let’s find out what we have received:

In [9]: flow_msg
    id: 'IDREF175210'
    prepared: '2020-05-15T10:16:48'
    receiver: 'not_supplied'
    sender: 'ECB'
  response: <Response [200]>
  DataflowDefinition (68): AME BKN BLS BNT BOP BSI BSP CBD CBD2 CCP CIS...

The string representation of the Message shows us a few things:

  • This is a Structure-, rather than DataMessage.

  • It contains 67 DataflowDefinition objects. Because we didn’t specify an ID of a particular data flow, we received the definitions for all data flows available from the ECB web service.

  • The first of these have ID attributes like ‘AME’, ‘BKN’, …

We could inspect these each individually using StructureMessage.dataflow attribute, a DictLike object that allows attribute- and index-style access:

In [10]: flow_msg.dataflow.BOP
Out[10]: <DataflowDefinition: 'BOP'='Euro Area Balance of Payments and International Investment Position Statistics'>

Convert metadata to pandas.Series

However, an easier way is to use pandasdmx to convert some of the information to a pandas.Series:

In [11]: dataflows = sdmx.to_pandas(flow_msg.dataflow)

In [12]: dataflows.head()
AME                                                AMECO
BKN                                 Banknotes statistics
BLS                       Bank Lending Survey Statistics
BNT        Shipments of Euro Banknotes Statistics (ESCB)
BOP    Euro Area Balance of Payments and Internationa...
dtype: object

In [13]: len(dataflows)
Out[13]: 68

to_pandas() accepts most instances and Python collections of pandasdmx.model objects, and we can use keyword arguments to control how each of these is handled. Under the hood, it calls pandasdmx.writer.write(). See the function documentation for details.

If we want to export the entire message content to pandas rather than selecting some resource such as dataflows as in the above example, the pandasdmx.message.Message.to_pandas() comes in handy.

As we are interested in exchange rate data, let’s use built-in Pandas methods to find an appropriate data flow:

In [14]: dataflows[dataflows.str.contains('exchange', case=False)]
EXR                              Exchange Rates
FXI                 Foreign Exchange Statistics
SEE    Securities exchange - Trading Statistics
dtype: object

We decide to look at ‘EXR’.

Some agencies, including ECB and INSEE, offer categorizations of data flows to help with this step. See this HOWTO entry.

Understand constraints

The CURRENCY and CURRENCY_DENOM dimensions of this DSD are both represented using the same CL_CURRENCY code list. In order to be reusable for as many data sets as possible, this code list is extensive and complete:

In [29]: len(exr_msg.codelist.CL_CURRENCY)
Out[29]: 359

However, the European Central Bank does not, in its ‘EXR’ data flow, commit to providing exchange rates between—for instance—the Congolose franc (‘CDF’) and Peruvian sol (‘PEN’). In other words, the values of (CURRENCY, CURRENCY_DENOM) that we can expect to find in ‘EXR’ is much smaller than the 359 × 359 possible combinations of two values from CL_CURRENCY.

How much smaller? Let’s return to explore the ContentConstraint that came with our metadata query:

In [30]: exr_msg.constraint.EXR_CONSTRAINTS
Out[30]: <ContentConstraint: 'EXR_CONSTRAINTS'='Constraints for the EXR dataflow.'>

# Get the content 'region' included in the constraint
In [31]: cr = exr_msg.constraint.EXR_CONSTRAINTS.data_content_region[0]

# Get the valid members for two dimensions
In [32]: c1 = sdmx.to_pandas(cr.member['CURRENCY'].values)

In [33]: len(c1)
Out[33]: 55

In [34]: c2 = sdmx.to_pandas(cr.member['CURRENCY_DENOM'].values)

In [35]: len(c2)
Out[35]: 58

# Explore the contents
# Currencies that are valid for CURRENCY_DENOM, but not CURRENCY
In [36]: c2 - c1

# The opposite:
In [37]: c1 - c2
Out[37]: {'E0', 'E5', 'E7', 'E8', 'EGP', 'H10', 'H11', 'H37', 'H42', 'H7', 'H8'}

# Check certain contents
In [38]: {'CDF', 'PEN'} < c1 | c2
Out[38]: False

In [39]: {'USD', 'JPY'} < c1 & c2
Out[39]: True

We also see that ‘USD’ and ‘JPY’ are valid values along both dimensions.

Attribute names and allowed values can be obtained in a similar fashion.

Select and query data from a dataflow

Next, we will query for some data. The step is simple: call Request.get() with resource_type=’data’ as the first argument, or the alias Request.data().

First, however, we describe some of the many options offered by SDMX and pandSDMX for data queries.

Choose a data format

Web services offering SDMX-ML–formatted DataMessages can return them in one of two formats:

Generic data

use XML elements that explicitly identify whether values associated with an Observation are dimensions, or attributes.

For example, in the ‘EXR’ data flow, the XML content for the CURRENCY_DENOM dimension and for the OBS_STATUS attribute are stored differently:

    <!-- NB. Other dimensions omitted. -->
    <generic:Value value="EUR" id="CURRENCY_DENOM"/>
    <!-- … -->
  <generic:ObsValue value="0.82363"/>
    <!-- NB. Other attributes omitted. -->
    <generic:Value value="A" id="OBS_STATUS"/>
    <!-- … -->
Structure-specific data

use a more concise format:

<!-- NB. Other dimensions and attributes omitted: -->

This can result in much smaller messages. However, because this format does not distinguish dimensions and attributes, it cannot be properly parsed by pandaSDMX without separately obtaining the data structure definition.

pandaSDMX adds appropriate HTTP headers for retrieving structure-specific data (see implementation notes). In general, to minimize queries and message size:

  1. First query for the DSD associated with a data flow.

  2. When requesting data, pass the obtained object as the dsd= argument to Request.get() or Request.data().

This allows pandaSDMX to retrieve structure-specific data whenever possible. It can also avoid an additional request when validating data query keys (below).

Construct a selection key for a query

SDMX web services can offer access to very large data flows. Queries for all the data in a data flow are not usually necessary, and in some cases servers will refuse to respond. By selecting a subset of data, performance is increased.

The SDMX REST API offers two ways to narrow a data request:

  • specify a key, i.e. values for 1 or more dimensions to be matched by returned Observations and SeriesKeys. The key is included as part of the URL constructed for the query. Using pandaSDMX, a key is specified by the key= argument to Request.get.

  • limit the time period, using the HTTP parameters ‘startPeriod’ and ‘endPeriod’. Using pandaSDMX, these are specified using the params= argument to Request.get.

From the ECB’s dataflow on exchange rates, we specify the CURRENCY dimension to contain either of the codes ‘USD’ or ‘JPY’. The documentation for Request.get() describes the multiple forms of the key argument and the validation applied. The following are all equivalent:

In [40]: key = dict(CURRENCY=['USD', 'JPY'])

In [41]: key = '.USD+JPY...'

We also set a start period to exclude older data:

In [42]: params = dict(startPeriod='2016')

Another way to validate a key against valid codes are series-key-only datasets, i.e. a dataset with all possible series keys where no series contains any observation. pandaSDMX supports this validation method as well. However, it is disabled by default. Pass series_keys=True to the Request method to validate a given key against a series-keys only dataset rather than the DSD.

Query data

Finally, we request the data in generic format:

In [43]: import sys

In [44]: ecb = sdmx.Request('ECB', backend='memory')

In [45]: data_msg = ecb.data('EXR', key=key, params=params)

# Generic data was returned
In [46]: data_msg.response.headers['content-type']
Out[46]: 'application/vnd.sdmx.genericdata+xml; version=2.1'

# Number of bytes in the cached response
In [47]: bytes1 = sys.getsizeof(ecb.session.cache.responses.popitem()[1][0]._content)

In [48]: bytes1
Out[48]: 556873

To demonstrate a query for a structure-specific data set, we pass the DSD obtained in the previous section:

In [49]: ss_msg = ecb.data('EXR', key=key, params=params, dsd=dsd)

# Structure-specific data was requested and returned
In [50]: ss_msg.response.request.headers['accept']
Out[50]: 'application/vnd.sdmx.structurespecificdata+xml;version=2.1'

In [51]: ss_msg.response.headers['content-type']
Out[51]: 'application/vnd.sdmx.structurespecificdata+xml; version=2.1'

# Number of bytes in the cached response
In [52]: bytes2 = sys.getsizeof(ecb.session.cache.responses.popitem()[1][0]._content)

In [53]: bytes2 / bytes1
Out[53]: 0.34325600271516127

The structure-specific message is a fraction of the size of the generic message.

In [54]: data = data_msg.data[0]

In [55]: type(data)
Out[55]: pandasdmx.model.GenericDataSet

In [56]: len(data.series)
Out[56]: 16

In [57]: list(data.series.keys())[5]

In [58]: set(series_key.FREQ for series_key in data.series.keys())
{<KeyValue: FREQ=A>,
 <KeyValue: FREQ=D>,
 <KeyValue: FREQ=H>,
 <KeyValue: FREQ=M>,
 <KeyValue: FREQ=Q>}

This dataset thus comprises 16 time series of several different period lengths. We could have chosen to request only daily data in the first place by providing the value ‘D’ for the FREQ dimension. In the next section we will show how columns from a dataset can be selected through the information model when writing to a pandas object.

Convert data to pandas

Select columns using the model API

As we want to write data to a pandas DataFrame rather than an iterator of pandas Series, we avoid mixing up different frequencies as pandas may raise an error when passed data with incompatible frequencies. Therefore, we single out the series with daily data. to_pandas() method accepts an optional iterable to select a subset of the series contained in the dataset. Thus we can now generate our pandas DataFrame from daily exchange rate data only:

In [59]: import pandas as pd

In [60]: daily = [s for sk, s in data.series.items() if sk.FREQ == 'D']

In [61]: cur_df = pd.concat(sdmx.to_pandas(daily))

In [62]: cur_df.shape
Out[62]: (2230,)

In [63]: cur_df.tail()
D     USD       EUR             SP00      A           2020-05-08     1.0843
                                                      2020-05-11     1.0824
                                                      2020-05-12     1.0858
                                                      2020-05-13     1.0875
                                                      2020-05-14     1.0792
Name: value, dtype: float64

Convert dimensions to pandas.DatetimeIndex or PeriodIndex

SDMX datasets often have a Dimension with a name like TIME_PERIOD. To ease further processing of time-series data read from SDMX messages, write_dataset() provides a datetime argument to convert these into pandas.DatetimeIndex and PeriodIndex classes.

For multi-dimensional datasets, write_dataset() usually returns a pandas.Series with a MultiIndex that has one level for each dimension. However, MultiIndex and DatetimeIndex/PeriodIndex are incompatible; it is not possible to use pandas’ date/time features for just one level of a MultiIndex (e.g. TIME_PERIOD) while using other types for the other levels/dimensions (e.g. strings for CURRENCY).

For this reason, when the datetime argument is used, write_dataset() returns a DataFrame: the DatetimeIndex/PeriodIndex is used along axis 0, and all other dimensions are collected in a MultiIndex on axis 1.

An example, using the same data flow as above:

In [64]: key = dict(CURRENCY_DENOM='EUR', FREQ='M', EXR_SUFFIX='A')

In [65]: params = dict(startPeriod='2019-01', endPeriod='2019-06')

In [66]: data = ecb.data('EXR', key=key, params=params).data[0]

Without date-time conversion, to_pandas() produces a MultiIndex:

In [67]: sdmx.to_pandas(data)
M     ARS       EUR             SP00      A           2019-01        42.736877
                                                      2019-02        43.523655
                                                      2019-03        46.479714
                                                      2019-04        48.520795
                                                      2019-05        50.155077
      ZAR       EUR             SP00      A           2019-02        15.687945
                                                      2019-03        16.250743
                                                      2019-04        15.895875
                                                      2019-05        16.137123
                                                      2019-06        16.474880
Name: value, Length: 306, dtype: float64

With date-time conversion, it produces a DatetimeIndex:

In [68]: df1 = sdmx.to_pandas(data, datetime='TIME_PERIOD')

In [69]: df1.index
DatetimeIndex(['2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01',
               '2019-05-01', '2019-06-01'],
              dtype='datetime64[ns]', name='TIME_PERIOD', freq=None)

In [70]: df1
FREQ                    M                    ...                                
CURRENCY              ARS       AUD     BGN  ...        TWD       USD        ZAR
CURRENCY_DENOM        EUR       EUR     EUR  ...        EUR       EUR        EUR
EXR_TYPE             SP00      SP00    SP00  ...       SP00      SP00       SP00
EXR_SUFFIX              A         A       A  ...          A         A          A
TIME_PERIOD                                  ...                                
2019-01-01      42.736877  1.597514  1.9558  ...  35.201205  1.141641  15.816950
2019-02-01      43.523655  1.589500  1.9558  ...  34.963125  1.135115  15.687945
2019-03-01      46.479714  1.595890  1.9558  ...  34.877605  1.130248  16.250743
2019-04-01      48.520795  1.580175  1.9558  ...  34.676925  1.123825  15.895875
2019-05-01      50.155077  1.611641  1.9558  ...  34.967468  1.118459  16.137123
2019-06-01      49.506670  1.626430  1.9558  ...  35.332025  1.129340  16.474880

[6 rows x 51 columns]

Use the advanced functionality to specify a dimension for the frequency of a PeriodIndex, and change the orientation so that the PeriodIndex is on the columns:

In [71]: df2 = sdmx.to_pandas(
   ....:   data,
   ....:   datetime=dict(dim='TIME_PERIOD', freq='FREQ', axis=1))

In [72]: df2.columns
Out[72]: PeriodIndex(['2019-01', '2019-02', '2019-03', '2019-04', '2019-05', '2019-06'], dtype='period[M]', name='TIME_PERIOD', freq='M')

In [73]: df2
TIME_PERIOD                                       2019-01  ...       2019-06
CURRENCY CURRENCY_DENOM EXR_TYPE EXR_SUFFIX                ...              
ARS      EUR            SP00     A              42.736877  ...     49.506670
AUD      EUR            SP00     A               1.597514  ...      1.626430
BGN      EUR            SP00     A               1.955800  ...      1.955800
BRL      EUR            SP00     A               4.269982  ...      4.360035
CAD      EUR            SP00     A               1.519614  ...      1.501140
CHF      EUR            SP00     A               1.129700  ...      1.116705
CNY      EUR            SP00     A               7.750350  ...      7.793685
CZK      EUR            SP00     A              25.650091  ...     25.604800
DKK      EUR            SP00     A               7.465736  ...      7.466945
DZD      EUR            SP00     A             135.144305  ...    134.583205
E0       EUR            EN00     A             101.313160  ...    101.231764
                        ERC0     A             103.563473  ...    103.413053
                        ERP0     A              98.820523  ...     98.124339
E5       EUR            EN00     A              97.817977  ...     97.879487
                        ERC0     A              92.193201  ...     91.863808
                        ERP0     A              92.608999  ...     92.149971
E7       EUR            EN00     A             117.320745  ...    117.412186
                        ERC0     A              92.702122  ...     92.221965
E8       EUR            EN00     A             101.461159  ...    101.379644
                        ERC0     A             103.298716  ...    103.148680
                        ERP0     A              99.203378  ...     98.504496
GBP      EUR            SP00     A               0.886030  ...      0.891073
H37      EUR            NN00     A             107.254509  ...    107.136919
                        NRC0     A              98.789722  ...     99.392810
H42      EUR            NN00     A             108.719167  ...    108.577679
                        NRC0     A              95.741332  ...     96.272071
HKD      EUR            SP00     A               8.952745  ...      8.838280
HRK      EUR            SP00     A               7.428550  ...      7.407885
HUF      EUR            SP00     A             319.800455  ...    322.558500
IDR      EUR            SP00     A           16164.770000  ...  16060.272000
ILS      EUR            SP00     A               4.207545  ...      4.062430
INR      EUR            SP00     A              80.798273  ...     78.407800
ISK      EUR            SP00     A             136.659091  ...    140.820000
JPY      EUR            SP00     A             124.341364  ...    122.080500
KRW      EUR            SP00     A            1281.459091  ...   1325.280500
MAD      EUR            SP00     A              10.882377  ...     10.847505
MXN      EUR            SP00     A              21.898509  ...     21.782680
MYR      EUR            SP00     A               4.700118  ...      4.696800
NOK      EUR            SP00     A               9.763105  ...      9.746480
NZD      EUR            SP00     A               1.685018  ...      1.711855
PHP      EUR            SP00     A              59.882455  ...     58.425100
PLN      EUR            SP00     A               4.291595  ...      4.263505
RON      EUR            SP00     A               4.706182  ...      4.725005
RUB      EUR            SP00     A              76.305455  ...     72.402775
SEK      EUR            SP00     A              10.268541  ...     10.626295
SGD      EUR            SP00     A               1.548614  ...      1.539010
THB      EUR            SP00     A              36.318273  ...     35.138600
TRY      EUR            SP00     A               6.136482  ...      6.561920
TWD      EUR            SP00     A              35.201205  ...     35.332025
USD      EUR            SP00     A               1.141641  ...      1.129340
ZAR      EUR            SP00     A              15.816950  ...     16.474880

[51 rows x 6 columns]


For large datasets, parsing datetimes may reduce performance.

Work with files

Request.get() accepts the optional keyword argument tofile. If given, the response from the web service is written to the specified file, and the parse Message returned.

New in version 0.2.1.

read_sdmx() can be used to load SDMX messages stored in local files:

# Use an example ('specimen') file from the pandaSDMX test suite
In [74]: from pandasdmx.tests.data import specimen

# …with time-series exchange rate data from the EU Central Bank
In [75]: with specimen('ECB_EXR/ng-ts.xml') as f:
   ....:     sdmx.read_sdmx(f)

Handle errors

Message.response carries the requests.Response.status_code attribute; in the successful queries above, the status code is 200. The SDMX web services guidelines explain the meaning of other codes. In addition, if the SDMX server has encountered an error, it may return a Message with a footer containing explanatory notes. pandaSDMX exposes footer content as Message.footer and Footer.text.


pandaSDMX raises only HTTP errors with status code between 400 and 499. Codes >= 500 do not raise an error as the SDMX web services guidelines define special meanings to those codes. The caller must therefore raise an error if needed.