Search code examples
pythonpandassdmx

How to set attributes in pandasdmx


The "pandasdmx" documentation has an example of how to set dimensions, but no how to set attributes. I tried passing "key" to the variable, but an error occurs. Perhaps setting attributes requires a different syntax? At the end of my sample code, I printed out a list of dimensions and attributes.

import pandasdmx as sdmx
import pandas as pd

ecb = sdmx.Request('ECB')
key = dict(FREQ=['M'], SEC_ITEM=['F51100'], REF_AREA = ['I8'], SEC_ISSUING_SECTOR = ['1000'])
params = dict(startPeriod='2010-02', endPeriod='2010-02')
data_msg = ecb.data('SEC', key=key, params=params)

data = data_msg.data[0]
daily = [s for sk, s in data.series.items() if sk.FREQ == 'M']
cur_df = pd.concat(sdmx.to_pandas(daily)).unstack()
print(cur_df.to_string())

flow_msg = ecb.dataflow()
dataflows = sdmx.to_pandas(flow_msg.dataflow)
exr_msg = ecb.dataflow('SEC')
exr_flow = exr_msg.dataflow.SEC
dsd = exr_flow.structure

dimen = dsd.dimensions.components
attrib = dsd.attributes.components
print(dimen)
print(attrib)

Solution

  • To clarify, what you're asking here is not “how to set attributes” but “how to include attributes when converting (or ‘writing’) an SDMX DataSet to a pandas DataFrame”.

    Using sdmx1,¹ see the documentation for the attribute parameter to the write_dataset() function. This is the function that is ultimately called by to_pandas() when you pass a DataSet object as the first argument.

    In your sample code, the key line is:

    sdmx.to_pandas(daily)
    

    You might try instead:

    sdmx.to_pandas(data_msg.data[0], attributes="dsgo")
    

    Per the documentation, the string "dsgo" means “include in additional columns all attributes attached at the dataset, series key, group, and observation levels.”


    ¹ sdmx1 is a fork of pandaSDMX with many enhancements and more regular releases.