Skip to main content
NERD LAB

undata

UN data in Python, without the guessing.

CI Live API contract PyPI Python 3.11+ License: MIT

Nine use cases, with real output →

Type a country's name, get a table. undata is a Python package for the UN System Data Commons, which holds statistics from 26 UN System entities. It is written for colleagues who need numbers for a report, not a course in APIs.

Alpha. It works today; the API may still change before 1.0. Unofficial. A Nerd Lab experiment, not affiliated with or endorsed by the United Nations. The data belongs to the agencies that publish it.

Three lines to a spreadsheet

pip install undata
import undata as un

table = un.get("undata/sdg/SH_STA_MORT.SEX--F", ["Bangladesh", "Viet Nam"])
un.save(table, "maternal-mortality.xlsx")  # data on one sheet, citations on the next

In Google Colab, run !pip install undata in the first cell. For the latest unreleased changes: pip install "git+https://github.com/nerd-factory/undata".

Don't know the indicator's id? un.search("maternal mortality") lists matching indicators with their names. Copy the id from the first column.

Everything on one screen

Function What it does
un.search("words") Find indicators by what they measure: UN collections, one row per indicator, with coverage.
un.about(indicator) What an indicator is, whose it is, its unit, years, and its SDG metadata link.
un.get(indicator, countries, years) The numbers: one row per country and year, with unit and source. by="sex" for a breakdown, wide=True for one column per indicator.
un.latest(indicator, countries) Each country's most recent value. within=10 keeps only values from the past ten years and names the countries whose latest is older.
un.coverage(table) How complete a table is: who has a value, who was too old, who has none, and which years.
un.totals(indicator, ["World", "LDCs"]) The official figure for a region or group, as the agency published it.
un.change(indicator, countries, 2015, 2023) Before, after, the difference and the percentage change.
un.compare({"a": id1, "b": id2}, countries) Indicators side by side, with who has which spelled out.
un.breakdowns(indicator) Every breakdown: by sex, age, wealth and so on.
un.countries(region) · un.regions() The UN M49 list: countries and areas with their UN names, M49 regions, and the LDC, LLDC and SIDS groups.
un.sdg.goals() · targets(16) · indicators("16.1") · series("16.1.2") Walk from an SDG goal down to indicators you can fetch.
un.describe(table) Plain-English sentences about a table, worked out from its numbers.
un.chart(table) A chart with its source underneath, for a slide. Needs pip install "undata[charts]".
un.save(table, "file.xlsx") · un.cite(table) Excel or CSV with sources; citations for a report.
un.for_ai(table, task="brief") The table with its sources, caveats and a task, to paste into an AI assistant.
un.ai_pack(table, "pack.zip") The data, a data dictionary and a prompt, to upload to an AI assistant.
un.powerquery(indicator, countries) Power Query code that fetches the same data in Power BI or Excel.
un.ask("a question") What data.un.org's search box decided, including whether it guessed the place.
un.configure(cache=True) Keep responses on disk, for slow connections and notebooks that run again.

Wherever a function asks for countries you can write names the way you would in an email ("Viet Nam", "Vietnam", "VNM"), a list of them, a region such as "Southern Asia" or "Africa", or a UN group: "LDCs", "LLDCs", "SIDS". Wherever it asks for an indicator, an SDG number such as "3.1.1" works when one series reports it. Every function explains itself: help(un.get).

From a terminal

The same, without writing Python:

undata search "maternal mortality"
undata totals 3.1.1 World LDCs SIDS --latest
undata change 3.1.1 "Southern Asia" --start 2015 --end 2023 --describe
undata get 3.1.1 LDCs --years 2015-2023 --to maternal-mortality.xlsx
undata powerquery 3.1.1 "Southern Asia" > query.m

Every table has the same columns:

country  iso3  year  value  unit  indicator_name  indicator  source  source_url

The country list is the UN's own

Countries, regions and groups come from UN M49, the UN Statistics Division's standard, shipped with the package and checked against the UN's current table every night. Tables use official UN names, regions follow M49, and the LDC, LLDC and SIDS groups are the current UN lists (44, 32 and 53 countries), not the platform's older copies. Places the platform holds outside M49 are left out of every list, region and group; they can still be asked for by platform code, such as country/XKS.

Taking it to an AI assistant

table = un.latest("3.1.1", "LDCs", within=10)
un.ai_pack(table, "maternal-mortality.zip")  # upload, then paste prompt.md
print(un.for_ai(table, task="brief"))  # or paste this as one message

The pack holds data.csv, metadata.md (what each column means; each indicator's unit, source and citation; how complete the data is; the caveats) and prompt.md (rules that keep the assistant to these numbers, and a task). Tasks: explain, brief, trends, gaps, chart.

The careful parts are built in

  • It never guesses a place. data.un.org's search box answers a question about "Bengal" with figures for the United States. undata stops and says it does not know "Bengal".
  • Gaps are named, never zero. Countries without data are left out of the table and listed in a MissingDataWarning, so a chart cannot show them as zeros.
  • UN names in, UN names out. Tables say Türkiye, Viet Nam and Côte d'Ivoire, whatever spelling you typed.
  • The source is on every row. The same statistic can come from several agencies with different numbers.
  • Mixed years are flagged. "Latest" means each country's own latest year; a MixedYearsWarning says when those differ.
  • It works where other clients fail. The platform's firewall blocks Google's official datacommons-client (any POST body containing "select") and any GET request that mentions Western Sahara, Greece or Slovenia (ESH, GRC, SVN, mistaken for command names). undata routes around all of them.

Each behaviour is pinned by a test in tests/live/ that runs against the real platform every night and opens an issue if the platform changes. Most of them were found while writing the UN Data Commons Field Guide.

Use cases

Nine requests as they arrive in UN work, each a runnable script in examples/ that ends with a sentence ready for the report:

# Who What
01 Resident Coordinator's Office A briefing note: Chad against its Sahel neighbours
02 SDG reporting Maternal mortality in Southern Asia since 2015, against the Target 3.1 level
03 Gender focal point Women in parliament in Eastern Africa: who has passed 30%, who reached parity
04 Humanitarian coordination Refugees by country of origin, in number and per person
05 Peace and security Conflict deaths against homicide, and who reports at all
06 Statistics focal point One statistic, three agencies, two numbers: what to cite
07 Publications team A regional data pack in Excel, with citations and gaps on their own sheets
08 Monitoring and evaluation A dashboard file that refreshes itself on a schedule
09 Communications Checking what data.un.org's search box decided before sharing its chart

They run against the live platform every night. The use-cases page shows each with its code, a chart, the real output and the sentence.

For developers

The friendly functions sit on three layers you can use directly: undata.Transport (the only code that touches the network), undata._endpoints (one thin wrapper per API route), and undata.dcid (the identifier grammar). PLAN.md covers the design and the road to 1.0.

git clone https://github.com/nerd-factory/undata && cd undata
uv sync                     # Python 3.11+, installs dev tools
uv run pytest               # unit tests and doctests, offline
uv run pytest -m live       # pins the live platform's behaviour (network)
uv run ruff check . && uv run ruff format --check . && uv run mypy

See CONTRIBUTING.md. Questions that feel too basic are welcome in Nerd Lab Discussions; they are not too basic.

Licence

Code: MIT. Data fetched with it is governed by the UN Data Commons terms of use and by each contributing agency's own terms.

Metadata

Release files for undata 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for undata 0.2.0
File Size Uploaded
undata-0.2.0.tar.gz 84.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for undata 0.2.0
File Interpreter ABI Platform
undata-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 151.0 kB

Release files / undata-0.2.0.tar.gz

Download URL undata-0.2.0.tar.gz
Size 84.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b2b600f7088595bb490a1874e6fab5c0233dfa7125d3ece166b755edd8c87acf
BLAKE2b-256 checksum
How to use checksums
edf84fc4f383929077e2dd8810836d4fb0888894e05c88384a6b1b5461d2e514
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.

Transparency log

Release files / undata-0.2.0-py3-none-any.whl

Download URL undata-0.2.0-py3-none-any.whl
Size 66.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18f1fd5a34e5fa1e3e586fedf206ca9ae540da5e54b55336a83dfed5056c06fa
BLAKE2b-256 checksum
How to use checksums
27f718519fee5dc6bcaea5fb8dc5170b0c0fd9b57b8fabf10cf63817606b6060
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.

Transparency log

Release history Release notifications | RSS feed

0.4.0

2 release files

0.3.0

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page