Skip to main content

Python library for downloading UNICEF indicators via SDMX API

Reason this release was yanked:

Superseded by 2.1.1 which includes required disclaimers and acknowledgements

Project description

unicefData - Python Package

Python Tests Python 3.9+ License: MIT

Python component of the trilingual unicefData library for downloading UNICEF SDG indicators via SDMX API

This is the Python implementation of the unicefData package. For other implementations, see the links below.

Other languages: R | Stata | Main README


Installation

pip install unicefdata

For development:

git clone https://github.com/unicef-drp/unicefData.git
cd unicefData/python
pip install -e ".[dev]"

Quick Start

Search for Indicators

from unicefdata import search_indicators, list_categories

# Search by keyword
search_indicators("mortality")
search_indicators("stunting")

# List all categories
list_categories()

# Search within a category
search_indicators(category="CME")
search_indicators("rate", category="CME")

Download Data

from unicefdata import unicefData

# Fetch under-5 mortality (dataflow auto-detected)
df = unicefData(
    indicator="CME_MRY0T4",
    countries=["ALB", "USA", "BRA"],
    year="2015:2023"  # Range, single year, or list
)

print(df.head())

View Dataflow Schema

from unicefdata import dataflow_schema, print_dataflow_schema

schema = dataflow_schema("CME")
print_dataflow_schema(schema)

Post-Production Options

Output Formats

# Long format (default)
df = unicefData(indicator="CME_MRY0T4", format="long")

# Wide format - years as columns
df = unicefData(indicator="CME_MRY0T4", format="wide")

# Wide indicators - indicators as columns
df = unicefData(
    indicator=["CME_MRY0T4", "NT_ANT_HAZ_NE2_MOD"],
    format="wide_indicators"
)

Latest Value Per Country

df = unicefData(indicator="CME_MRY0T4", latest=True)

Most Recent Values (MRV)

df = unicefData(indicator="CME_MRY0T4", mrv=3)

Circa (Nearest Year)

df = unicefData(indicator="NT_ANT_HAZ_NE2", year=2015, circa=True)

Add Metadata

df = unicefData(
    indicator="CME_MRY0T4",
    add_metadata=["region", "income_group", "continent"]
)

Combining Options

df = unicefData(
    indicator=["CME_MRY0T4", "NT_ANT_HAZ_NE2_MOD"],
    format="wide_indicators",
    latest=True,
    add_metadata=["region", "income_group"],
    dropna=True
)

API Reference

unicefData()

Main function for fetching UNICEF indicator data.

Parameter Type Default Description
indicator str/list required Indicator code(s)
dataflow str auto-detect SDMX dataflow ID
countries list None (all) ISO3 country codes
year int/str/list None (all) Year(s)
circa bool False Find closest year
sex str "_T" Sex filter
max_retries int 3 Retry attempts

Post-Production Parameters

Parameter Type Default Description
format str "long" "long", "wide", "wide_indicators"
latest bool False Keep only latest per country
mrv int None Keep N most recent values
add_metadata list None Metadata to add
dropna bool False Remove missing values
simplify bool False Keep only essential columns

UNICEFSDMXClient (Advanced)

from unicefdata import UNICEFSDMXClient

client = UNICEFSDMXClient()

# Fetch single indicator
df = client.fetch_indicator(
    "CME_MRY0T4",
    countries=["ALB", "USA"],
    start_year=2015,
    end_year=2023
)

# Fetch multiple indicators
df = client.fetch_multiple_indicators(
    ["CME_MRY0T4", "NT_ANT_HAZ_NE2_MOD"],
    countries=["ALB", "USA"],
    combine=True
)

Other Functions

Function Description
search_indicators(query, category, limit) Search indicators
list_categories() List all categories
list_dataflows() List available dataflows
dataflow_schema(dataflow) Get dataflow schema
clear_cache() Clear all 5 cache layers

Time Period Handling

Monthly periods are converted to decimal years:

Original Decimal Calculation
2020 2020.0 Integer year
2020-01 2020.0833 2020 + 1/12
2020-06 2020.5000 2020 + 6/12

Common Indicators

Child Mortality (SDG 3.2)

  • CME_MRM0 - Neonatal mortality rate
  • CME_MRY0T4 - Under-5 mortality rate

Nutrition (SDG 2.2)

  • NT_ANT_HAZ_NE2_MOD - Stunting prevalence
  • NT_ANT_WHZ_NE2 - Wasting prevalence

Immunization (SDG 3.b)

  • IM_DTP3 - DTP3 coverage
  • IM_MCV1 - Measles coverage

WASH (SDG 6)

  • WS_PPL_W-SM - Safely managed water
  • WS_PPL_S-SM - Safely managed sanitation

Child Protection

  • PT_CHLD_Y0T4_REG - Birth registration
  • PT_F_20-24_MRD_U18_TND - Child marriage

Error Handling

from unicefdata import SDMXNotFoundError, SDMXBadRequestError, SDMXTimeoutError

try:
    df = unicefData(indicator="INVALID_CODE")
except SDMXNotFoundError as e:
    print(f"Indicator not found: {e}")
except SDMXBadRequestError as e:
    print(f"Invalid request: {e}")
except SDMXTimeoutError as e:
    print(f"Request timed out: {e}")

Configurable Timeout

from unicefdata import UNICEFSDMXClient

# Set custom timeout (default: 60s)
client = UNICEFSDMXClient(timeout=120)

Troubleshooting

Connection Errors

# Increase retry attempts
df = unicefData(indicator="CME_MRY0T4", max_retries=5)

Stale Cache

from unicefdata import clear_cache
clear_cache()  # Clears all 5 cache layers

Examples

See examples/ folder:

  • 00_quick_start.py - Basic usage
  • 01_indicator_discovery.py - Finding indicators
  • 02_sdg_indicators.py - SDG queries
  • 03_data_formats.py - Output formats
  • 04_metadata_options.py - Metadata enrichment
  • 05_advanced_features.py - Advanced options

Version History

See CHANGELOG.md for complete changelog.


Dependencies

  • pandas
  • requests
  • pyyaml

License

MIT License - See LICENSE

Author

Joao Pedro Azevedo Chief Statistician, UNICEF Data and Analytics Section Email: jpazevedo@unicef.org Website: jpazvd.github.io

Contributing

See CONTRIBUTING.md for detailed guidelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unicefdata-2.1.0.tar.gz (348.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unicefdata-2.1.0-py3-none-any.whl (414.7 kB view details)

Uploaded Python 3

File details

Details for the file unicefdata-2.1.0.tar.gz.

File metadata

  • Download URL: unicefdata-2.1.0.tar.gz
  • Upload date:
  • Size: 348.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for unicefdata-2.1.0.tar.gz
Algorithm Hash digest
SHA256 d300f8b07f38ae4d6bf74b059936b74b9538322392c5c1d4fc808973c676f858
MD5 2cc82c7459953832f5e9249d450bb0bf
BLAKE2b-256 4f5932a916298e08774c46f0f094d3154e9b2512998ccb183d183864173051bc

See more details on using hashes here.

File details

Details for the file unicefdata-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: unicefdata-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 414.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for unicefdata-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c2bd2cf61861be8d88183907c6778777b2e464f5967c8f80057b439ad7a3d41
MD5 b5e0c3a84363cb85f218780b6785d0b2
BLAKE2b-256 5c16210ca575fdfbc8b6c33004a5bf1b8934191993f1e7eb384d58398d6b053a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page