Convert from/to SDMX¶
sdmx.convert and sdmx.convert.common
provide generic and extensible features for converting:
from
sdmx.messageandsdmx.modelobjects to arbitrary Python data structures, orfrom arbitrary Python data structures to
sdmxobjects.
The included sdmx.convert.pandas, PandasConverter, and to_pandas()
build on these features to provide conversion to types from the pandas package.
This conversion is not described by the SDMX standards;
in other words, it is particular to the sdmx package.
In contrast, submodules of sdmx.writer, to_csv(), and to_xml()
provide conversion to standard Standard formats.
User code can also subclass Converter or DispatchConverter
to convert to/from other Python types or non-standard formats.
- class sdmx.convert.common.DispatchConverter[source]¶
Base class for recursive converters.
Usage:
Create a subclass of this class.
Use
register()(in the same manner as Python’s built-infunctools.singledispatch()) to decorate functions that convert certain types ofsdmx.modelorsdmx.messageobjects.Call
convert()(including from inside other functions) to recursively convert objects.
Example
>>> from sdmx.convert.common import DispatchConverter >>> class CustomConverter(DispatchConverter): ... pass
>>> @CustomConverter.register ... def _(c: "CustomConverter", obj: sdmx.model.ItemScheme): ... ... code to convert an ItemScheme ... ... return result
>>> @CustomConverter.register ... def _(c: "CustomConverter", obj: sdmx.model.Codelist): ... ... code to convert a Codelist ... ... return result
convert.pandas: Convert to pandas objects¶
|
Convert SDMX messages and IM objects to |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
Other objects are converted as follows:
ComponentThe
idattribute of theconcept_identityis returned.DataMessageThe
DataSetor data sets within the Message are converted to pandas objects. Returns:pandas.Seriesorpandas.DataFrame, if obj has only one data set.list of (Series or DataFrame), if obj has more than one data set.
dictThe values of the mapping are converted individually. If the resulting values are
stror Series with indexes that share the same name, then they are converted to a Series, possibly with apandas.MultiIndex. Otherwise, aDictLikeis returned.DimensionDescriptorThe
componentsof the DimensionDescriptor are converted.listFor the following obj, returns Series instead of a
list:a list of
Observation: the Observations are converted usingconvert_dataset().a list with only 1
DataSet <.BaseDataSet(e.g. thedataattribute ofDataMessage): the Series for the single element is returned.a list of
SeriesKey: the key values (but no data) are returned.
NameableArtefactThe
nameattribute of obj is returned.
Todo
Support selection of language for conversion of
InternationalString.
Code reference¶
Convert sdmx.message and model objects to pandas objects.
- sdmx.convert.pandas.ALL_CONTENTS = {'category_scheme', 'codelist', 'concept_scheme', 'constraint', 'dataflow', 'organisation_scheme', 'structure'}[source]¶
TODO Retrieve this info from the StructureMessage class.
- class sdmx.convert.pandas.Column[source]¶
Representation of conversion of a column.
Todo
Unify with
reader.csv.Handler.
- class sdmx.convert.pandas.ColumnSpec(pc: PandasConverter | None = None, ds: BaseDataSet | None = None)[source]¶
Information about columns for conversion.
- convert_obs(obs: BaseObservation) list[source]¶
Convert a single Observation to a data row.
The items of the result correspond to the column names in
obs.
- class sdmx.convert.pandas.ComponentColumn(component: Component)[source]¶
A column taking its header from a
Component.
- class sdmx.convert.pandas.PandasConverter(format_options: CSVFormatOptions = <factory>, attributes: Attributes = <Attributes.none: 0>, constraint: ContentConstraint | None = None, dtype: type[np.generic] | type[ExtensionDtype] | str | None = <class 'numpy.float64'>, datetime_axis: int | str = -1, datetime_dimension: common.DimensionComponent | None = None, datetime_freq: PeriodFrequency | None = None, include: set[str] = <factory>, locale: str = 'en', datetime: InitVar = None, rtype: dataclasses.InitVar[str] = '', _columns: ColumnSpec = <factory>, _strict: bool = False, _unstack: list[str] = <factory>, _context: dict[str | type, ~typing.Any]=<factory>)[source]¶
Convert SDMX messages and IM objects to
pandas.DataFrameand similar.PandasConverter implements a dispatch pattern according to the type of the object to be converted. The attributes/arguments to the class control the conversion behaviour and return types.
- attributes: Attributes = 0[source]¶
Attributes to include.
- constraint: ContentConstraint | None = None[source]¶
If given, only Observations included by the constraint are returned.
- datetime: InitVar = None[source]¶
Trueto convert datetime.Deprecated since version 2.23.0: Use
datetime_axis,datetime_dimension, ordatetime_freq.
- datetime_axis: int | str = -1[source]¶
Axis on which to place a time dimension. One of:
-1: disabled.0, "index": first/index axis.1, "columns": second/columns axis.
- datetime_dimension: common.DimensionComponent | None = None[source]¶
Dimension to convert to
pandas.DatetimeIndex. Astrvalue is interpreted as a dimension ID.
- datetime_freq: PeriodFrequency | None = None[source]¶
Frequency for conversion to
pandas.PeriodIndex. Astrvalue is interpreted as one of the Period aliases.
- dtype[source]¶
Datatype for observation values. If
None, data values remainobject/str.alias of
float64
- format_options: CSVFormatOptions[source]¶
SDMX-CSV format options.
- get_components(kind) list[Component][source]¶
Return an appropriate list of dimensions or attributes.
- handle_datetime(value: Any) None[source]¶
Handle alternate forms of
datetime.If given, return a DataFrame with a
DatetimeIndexorPeriodIndexas the index and all other dimensions as columns. Valid datetime values include:bool: ifTrue, determine the time dimension automatically by detecting aTimeDimension.str: ID of the time dimension.Dimension: the matching Dimension is the time dimension.dict: advanced behaviour. Keys may include:axis ({0 or ‘index’, 1 or ‘columns’}): axis on which to place the time dimension (default: 0).
freq (
TrueorstrorDimension): producepandas.PeriodIndex. Ifstr, the ID of a Dimension containing a frequency specification. If a Dimension, the specified dimension is used for the frequency specification.Any Dimension used for the frequency specification is does not appear in the returned DataFrame.
- include: set[str][source]¶
iterable of str or str, optional One or more of the attributes of the StructureMessage (‘category_scheme’, ‘codelist’, etc.) to transform.
- Type:
- rtype: dataclasses.InitVar[str] = ''[source]¶
Return type for
convert_dataset()and similar methods.
- sdmx.convert.pandas.convert_datamessage(c: PandasConverter, obj: DataMessage)[source]¶
Convert
DataMessage.- Parameters:
rtype (
'compat'or'rows', optional) – Data type to return; defaultDEFAULT_RTYPE. See the HOWTO.kwargs – Passed to
convert_dataset()for each data set.
- Returns:
pandas.Seriesorpandas.DataFrame– if obj has only one data set.listof(:class:`pandas.Series`orpandas.DataFrame) – if obj has more than one data set.
- sdmx.convert.pandas.convert_dataset(c: PandasConverter, obj: BaseDataSet)[source]¶
Convert
DataSet.See the walkthrough for examples of using the datetime argument.
- Returns:
-
if
attributesis not'', a data frame with one row per Observation,valueas the first column, and additional columns for each attribute;if datetime is given, various layouts as described above; or
if _rtype (passed from
convert_datamessage()) is ‘compat’, various layouts as described in the HOWTO.
pandas.Serieswithpandas.MultiIndex– Otherwise.
-
- sdmx.convert.pandas.convert_itemscheme(c: PandasConverter, obj: ItemScheme)[source]¶
Convert
ItemScheme.- Parameters:
locale (
str, optional) – Locale for names to return.- Return type:
- sdmx.convert.pandas.convert_structuremessage(c: PandasConverter, obj: StructureMessage)[source]¶
Convert
StructureMessage.- Returns:
Keys are StructureMessage attributes; values are pandas objects.
- Return type: