Statistical resources

Dataframe handling

Apart from all the functions below, there are two special calls we can use in order to get a pandas DataFrame instead of the usual json API response:

Important

pip install pandas is required for this feature to work.

Guess you are looking for data about population under 18 in Canary Islands regarding its employment status. Once you get the resource identifier you could make this query:

>>> from istacpy.statisticalresources import queries

>>> response = queries.get_statisticalresources_queries_agency_resource(
...    agencyid='ISTAC',
...    resourceid='C00086B_000006',
...    as_dataframe=True
... )

>>> type(response)
istacpy.services.ResolvedAPIResponse

Note

Adding the parameter as_dataframe=True makes you get a ResolvedAPIResponse object.

This object provides two attributes: dataframe and codelists:

>>> response.dataframe
                MEDIDAS TIME_PERIOD SEXO      SITUACION_ECONOMICA_ICC VALORACION OBSERVACIONES
0                POBLACION     2018-Q4   _T       ACTUAL_BUSQUEDA_EMPLEO         _T       1791633
1                POBLACION     2018-Q4   _T       ACTUAL_BUSQUEDA_EMPLEO      IGUAL        782350
2                POBLACION     2018-Q4   _T       ACTUAL_BUSQUEDA_EMPLEO      MEJOR        272479
3                POBLACION     2018-Q4   _T       ACTUAL_BUSQUEDA_EMPLEO       PEOR        736804
4                POBLACION     2018-Q4   _T  EXPECTATIVA_BUSQUEDA_EMPLEO         _T       1791633
..                     ...         ...  ...                          ...        ...           ...
283  POBLACION__PORCENTAJE     2020-Q4    M       ACTUAL_BUSQUEDA_EMPLEO       PEOR         87.10
284  POBLACION__PORCENTAJE     2020-Q4    M  EXPECTATIVA_BUSQUEDA_EMPLEO         _T        100.00
285  POBLACION__PORCENTAJE     2020-Q4    M  EXPECTATIVA_BUSQUEDA_EMPLEO      IGUAL         19.25
286  POBLACION__PORCENTAJE     2020-Q4    M  EXPECTATIVA_BUSQUEDA_EMPLEO      MEJOR         19.35
287  POBLACION__PORCENTAJE     2020-Q4    M  EXPECTATIVA_BUSQUEDA_EMPLEO       PEOR         61.40

[288 rows x 6 columns]

>>> response.codelists
{'MEDIDAS': {'POBLACION': 'Población',
'POBLACION__PORCENTAJE': 'Población. Porcentaje'},
'TIME_PERIOD': {'2020-Q4': '2020 Cuarto trimestre',
'2020-Q2': '2020 Segundo trimestre',
'2019-Q4': '2019 Cuarto trimestre',
'2019-Q2': '2019 Segundo trimestre',
'2019-Q1': '2019 Primer trimestre',
'2018-Q4': '2018 Cuarto trimestre'},
'SEXO': {'_T': 'Ambos sexos', 'M': 'Hombres', 'F': 'Mujeres'},
'SITUACION_ECONOMICA_ICC': {'ACTUAL_BUSQUEDA_EMPLEO': 'Situación actual para encontrar o mejorar un puesto de trabajo',
'EXPECTATIVA_BUSQUEDA_EMPLEO': 'Expectativa futura para encontrar o mejorar un puesto de trabajo'},
'VALORACION': {'_T': 'Total',
'MEJOR': 'Mejor',
'IGUAL': 'Igual',
'PEOR': 'Peor'}}

Important

Codelists are mappings between codes and text descriptions. The returned response.codelists always contains spanish texts because they are most cases.

Recoding of downloaded dataset is as easy as:

>>> response.dataframe.replace(response.codelists)
                MEDIDAS            TIME_PERIOD         SEXO                            SITUACION_ECONOMICA_ICC VALORACION OBSERVACIONES
0                Población  2018 Cuarto trimestre  Ambos sexos  Situación actual para encontrar o mejorar un p...      Total       1791633
1                Población  2018 Cuarto trimestre  Ambos sexos  Situación actual para encontrar o mejorar un p...      Igual        782350
2                Población  2018 Cuarto trimestre  Ambos sexos  Situación actual para encontrar o mejorar un p...      Mejor        272479
3                Población  2018 Cuarto trimestre  Ambos sexos  Situación actual para encontrar o mejorar un p...       Peor        736804
4                Población  2018 Cuarto trimestre  Ambos sexos  Expectativa futura para encontrar o mejorar un...      Total       1791633
..                     ...                    ...          ...                                                ...        ...           ...
283  Población. Porcentaje  2020 Cuarto trimestre      Hombres  Situación actual para encontrar o mejorar un p...       Peor         87.10
284  Población. Porcentaje  2020 Cuarto trimestre      Hombres  Expectativa futura para encontrar o mejorar un...      Total        100.00
285  Población. Porcentaje  2020 Cuarto trimestre      Hombres  Expectativa futura para encontrar o mejorar un...      Igual         19.25
286  Población. Porcentaje  2020 Cuarto trimestre      Hombres  Expectativa futura para encontrar o mejorar un...      Mejor         19.35
287  Población. Porcentaje  2020 Cuarto trimestre      Hombres  Expectativa futura para encontrar o mejorar un...       Peor         61.40

[288 rows x 6 columns]

Tip

This process can also be made for statistical datasets.

istacpy.statisticalresources.queries

istacpy.statisticalresources.queries.get_statisticalresources_queries(lang='es', limit=25, offset=0, orderby='', query='')

Get queries

This function allows consulting all existing statistical queries.

Parameters
  • lang (string) – Language in which you want to get the answer.

  • limit (int) – Results limit. By default limit=25.

  • offset (int) – Displacement. Result from which it is returned. By default offset=0.

  • orderby (string) – Order. Possible values are ID ASC or ID DESC,

  • query (string) – Metadata query on which the searches can be built.

Examples

>>> get_statisticalresources_queries()
istacpy.statisticalresources.queries.get_statisticalresources_queries_agency(agencyid, lang='es', limit=25, offset=0, orderby='', query='')

Get queries (agencyID)

This function allows to consult all the queries maintained by a certain organization.

Parameters
  • agencyid (string) – Identifier of the maintainer organization of the resource. A possible value is ISTAC.

  • lang (string) – Language in which you want to get the answer.

  • limit (int) – Results limit. By default limit=25.

  • offset (int) – Displacement. Result from which it is returned. By default offset=0.

  • orderby (string) – Order. Possible values are ID ASC or ID DESC,

  • query (string) – Metadata query on which the searches can be built.

Examples

>>> get_statisticalresources_queries_agency(agencyid="ISTAC")
istacpy.statisticalresources.queries.get_statisticalresources_queries_agency_resource(agencyid, resourceid, fields='', lang='es', as_dataframe=False)

Get queries (agencyID / resourceID)

This function allows to obtain final data of a statistical query with a certain identifier and that also maintains a certain organization.

Parameters
  • agencyid (string) – Identifier of the maintainer organization of the resource. A possible value is ISTAC.

  • resourceid (string) – Resource identifier. A possible value is C00010A_000002.

  • fields (string) – Allows you to customize the response by excluding fields from it. The possible values are -metadata and -data.

  • lang (string) – Language in which you want to get the answer.

  • as_dataframe (bool) – If True, this function returns a namedtuple with: - dataframe: pandas dataframe built from API response. - codelists: mapping between codes and representations for each column.

Examples

>>> get_statisticalresources_queries_agency_resource(
...     agencyid="ISTAC",
...     resourceid="C00017A_000001"
... )

istacpy.statisticalresources.cubes

istacpy.statisticalresources.cubes.get_statisticalresources_datasets(lang='es', limit=25, offset=0, orderby='', query='')

Get datasets

This function allows consulting all existing statistical data cubes.

Parameters
  • lang (string) – Language in which you want to get the answer.

  • limit (int) – Results limit. By default limit=25.

  • offset (int) – Displacement. Result from which it is returned. By default offset=0.

  • orderby (string) – Order. Possible values are ID ASC or ID DESC,

  • query (string) – Metadata query on which the searches can be built.

Examples

>>> get_statisticalresources_datasets()
istacpy.statisticalresources.cubes.get_statisticalresources_datasets_agency(agencyid, lang='es', limit=25, offset=0, orderby='', query='')

Get datasets (agencyID)

This function allows to consult all the data sets maintained by a certain organization.

Parameters
  • agencyid (string) – Identifier of the maintainer organization of the resource. A possible value is ISTAC.

  • lang (string) – Language in which you want to get the answer.

  • limit (int) – Results limit. By default limit=25.

  • offset (int) – Displacement. Result from which it is returned. By default offset=0.

  • orderby (string) – Order. Possible values are ID ASC or ID DESC,

  • query (string) – Metadata query on which the searches can be built.

Examples

>>> get_statisticalresources_datasets_agency(agencyid="ISTAC")
istacpy.statisticalresources.cubes.get_statisticalresources_datasets_agency_resource(agencyid, resourceid, lang='es', limit=25, offset=0, orderby='', query='')

Get datasets (agencyID / resourceID)

This function allows to obtain all the versions of a statistical cube with a certain identifier and that also maintains a certain organization.

Parameters
  • agencyid (string) – Identifier of the maintainer organization of the resource. A possible value is ISTAC.

  • resourceid (string) – Resource identifier. A possible value is C00010A_000002.

  • lang (string) – Language in which you want to get the answer.

  • limit (int) – Results limit. By default limit=25.

  • offset (int) – Displacement. Result from which it is returned. By default offset=0.

  • orderby (string) – Order. Possible values are ID ASC or ID DESC,

  • query (string) – Metadata query on which the searches can be built.

Examples

>>> get_statisticalresources_datasets_agency_resource(
...     agencyid="ISTAC",
...     resourceid="C00010A_000002"
... )
istacpy.statisticalresources.cubes.get_statisticalresources_datasets_agency_resource_version(agencyid, resourceid, version, dim='', fields='', lang='es', as_dataframe=False)

Get datasets (agencyID / resourceID / version)

This function allows to obtain all the versions of a statistical cube with a certain identifier and that also maintains a certain organization.

Parameters
  • agencyid (string) – Identifier of the maintainer organization of the resource. A possible value is ISTAC.

  • resourceid (string) – Resource identifier. A possible value is C00010A_000002.

  • version (string) – Resource version. A possible value is 001.000.

  • dim (string) – Allows filtering the data obtained in the response. A example is TIME_PERIOD:2009|2010.

  • fields (string) – Allows you to customize the response by excluding fields from it. The possible values are -metadata and -data.

  • lang (string) – Language in which you want to get the answer.

  • as_dataframe (bool) – If True, this function returns a namedtuple with: - dataframe: pandas dataframe built from API response. - codelists: mapping between codes and representations for each column.

Examples

>>> get_statisticalresources_datasets_agency_resource_version(
...     agencyid="ISTAC",
...     resourceid="C00010A_000002",
...     version="001.000"
... )