Sample code
Here are some worked examples of the PDH.stat API being used for various reasons. Full details of each implementation are available in the provided Github links.
Many of these tasks can be simplified through the PDH.stat API's suite of plugins.

Get all dataflow IDs from PDH.stat API

This Python script returns a list of all existing dataflowIDs. This can be useful for applications which need to check for new/updated dataflows.
1
import requests
2
from bs4 import BeautifulSoup
3
4
base_url = 'https://stats-nsi-stable.pacificdata.org/rest/'
5
6
def get_endpoints(base_url):
7
"""
8
Get the dataflowIDs of all existing dataflows in PDH .Stat API
9
:param base_url: the API's base URL
10
:return: list of strings
11
"""
12
endpoints = []
13
resources_url = base_url + 'dataflow/all/all/latest?detail=full'
14
resp = requests.get(resources_url, verify=False)
15
soup = BeautifulSoup(resp.text, 'xml')
16
for name in soup.findAll('Dataflow'):
17
endpoints.append(name['id'])
18
return endpoints
19
20
df_ids = get_endpoints(base_url)
21
print(df_ids)
22
23
# Output: ['DF_COMMODITY_PRICES', 'DF_CPI', 'DF_CURRENCIES', 'DF_EMPLOYED', 'DF_EMPRATES', 'DF_GFS', 'DF_HHEXP', 'DF_IMTS', 'DF_LABEMP', 'DF_NATIONAL_ACCOUNTS', 'DF_NEET', 'DF_NMDI', 'DF_NMDI_DEV', 'DF_NMDI_EDU', 'DF_NMDI_FIS', 'DF_NMDI_HEA', 'DF_NMDI_INF', 'DF_NMDI_OTH', 'DF_NMDI_POP', 'DF_OVERSEAS_VISITORS', 'DF_POCKET', 'DF_POP_COAST', 'DF_POP_DENSITY', 'DF_POP_PROJ', 'DF_SDG', 'DF_SDG_01', 'DF_SDG_02', 'DF_SDG_03', 'DF_SDG_04', 'DF_SDG_05', 'DF_SDG_06', 'DF_SDG_07', 'DF_SDG_08', 'DF_SDG_09', 'DF_SDG_10', 'DF_SDG_11', 'DF_SDG_12', 'DF_SDG_13', 'DF_SDG_14', 'DF_SDG_15', 'DF_SDG_16', 'DF_SDG_17', 'DF_UIS', 'DF_VITAL']
Copied!

Get basic metadata about a dataflow from PDH.stat API

This Python script returns a dictionary with the title, agencyId, version for a given dataflowId. This can be useful for applications which harvest from PDH.stat or simply need to display information about a dataset/dataflow. The function can be used iteratively for information on more than one dataflow.
1
import requests
2
from bs4 import BeautifulSoup
3
4
base_url = 'https://stats-nsi-stable.pacificdata.org/rest/'
5
df = str(input('Enter dataflow ID: '))
6
# Or hard-code an example dataflow
7
# df = 'DF_SDG'
8
9
def basic_metadata(base_url, df):
10
"""
11
Get some basic metadata on a dataflow in PDH .Stat API
12
:param base_url: the API's base URL (string)
13
:param df: the dataflow ID (string) e.g. 'DF_SDG'
14
:return: dictionary of metadata key, value pairs
15
"""
16
meta_suffix = 'latest/?references=all&detail=referencepartial'
17
meta_url = '{}dataflow/all/{}/{}'.format(base_url, df, meta_suffix)
18
meta = requests.get(meta_url, verify=False)
19
soup = BeautifulSoup(meta.text, 'xml')
20
structure = soup.find('Dataflow', attrs= {'id': df})
21
meta_dict = {'dataflowId' : df}
22
meta_dict['title'] = structure.find('Name').text
23
meta_dict['agencyId'] = structure['agencyID']
24
meta_dict['version'] = structure['version']
25
return meta_dict
26
27
dict = basic_metadata(base_url, df)
28
print(dict)
29
30
# Output (assuming :param df is 'DF_SDG')
31
# {'dataflowId': 'DF_SDG', 'title': 'Sustainable Development Goals (all)', 'agencyId': 'SPC', 'version': '3.0'}
Copied!

Plot time series population data using the Python plugin with PDH .Stat API

This Python script demonstrates how the API can be accessed with the Python sdmx plugin. It makes a request for a filtered dataset of population projections for a specified number of countries. It then plots the results as a time series chart. It could be adapted to handle different countries, different time frames and other time series data too.
1
import pandas as pd
2
import matplotlib.pyplot as plt
3
import sdmx
4
5
# Set SPC as the Client
6
spc = sdmx.Client('SPC')
7
8
# Design key to fetch mid-year population estimates for New Caledonia, Fiji and American Samoa
9
# Sex and Age are set to _T which represents total/all
10
key = dict(GEO_PICT=['NC', 'FJ', 'AS'], INDICATOR='MIDYEARPOPEST', SEX='_T', AGE='_T')
11
12
# Set parameters to get data from 1970 to 2010
13
params = dict(startPeriod='1970', endPeriod='2010')
14
15
# Make the data request and pass the key and parameters
16
data = spc.data('DF_POP_PROJ', key=key, params=params)
17
18
# Load as dataframe
19
df = sdmx.to_pandas(data)
20
df = df.reset_index()
21
22
# Replace country codes with real names
23
df['GEO_PICT'] = df['GEO_PICT'].replace({'NC':'New Caledonia', 'AS': 'American Samoa', 'FJ': 'Fiji'})
24
25
# Group by country and plot the data as line charts
26
fig, ax = plt.subplots()
27
for key, grp in df.groupby(['GEO_PICT']):
28
ax = grp.plot(ax=ax, kind='line', x='TIME_PERIOD', y='value', label=key)
29
plt.title('Population estimates 1970 to 2010')
30
plt.xlabel('Year')
31
plt.ylabel('Population')
32
plt.legend(loc='best')
33
plt.show()
Copied!