Skip to main content

Read Human Mortality Database and Human Fertility Database data from the web

Project description

pyhmfd

A Python package for reading data from the Human Mortality Database (HMD), Human Fertility Database (HFD), Human Fertility Collection (HFC), Japanese Mortality Database (JMD), and Canadian Historical Mortality Database (CHMD).

Returns tidy pandas.DataFrame objects ready for analysis.

Credits

This package is a Python port of the R package HMDHFDplus by Tim Riffe, Jose Manuel Aburto, and contributors:

Riffe T, Aburto JM, et al. (2023). HMDHFDplus: Read Human Mortality Database and Human Fertility Database Data from the Web. R package. https://github.com/timriffe/HMDHFDplus

The authentication flow, URL patterns, parsing logic, and data-cleaning conventions are derived directly from that work. Licensed under GPL-2.0.

Supported databases

Database Short name Authentication
Human Mortality Database HMD account required
Human Fertility Database HFD account required
Human Fertility Collection HFC none
Japanese Mortality Database JMD none
Canadian Historical Mortality Database CHMD none

Installation

pip install pyhmfd

Or from source:

git clone https://github.com/filipeclduarte/pyhmfd.git
cd pyhmfd
pip install -e ".[dev]"

Credentials

For HMD and HFD you need a free account at their respective websites. Supply credentials via environment variables (recommended for scripts/CI):

export HMD_USER="your@email.com"
export HMD_PASSWORD="yourpassword"
export HFD_USER="your@email.com"
export HFD_PASSWORD="yourpassword"

Or pass them directly to the function, or let the package prompt interactively. Credentials entered interactively are offered to the system keyring for storage.

Quick start

import pyhmfd

# Human Mortality Database — needs credentials
df = pyhmfd.read_hmd_web("USA", "Mx_1x1")
df = pyhmfd.read_hmd_web("FRATNP", "Deaths_1x1", username="u@example.com", password="pw")

# Human Fertility Database — needs credentials
df = pyhmfd.read_hfd_web("USA", "asfrRR")

# Japanese Mortality Database — no auth
df = pyhmfd.read_jmd_web("01", "Deaths_1x1")   # 01 = Hokkaido

# Canadian Historical Mortality Database — no auth
df = pyhmfd.read_chmd_web("que", "Mx_1x1")     # que = Quebec

# Human Fertility Collection — no auth
df = pyhmfd.read_hfc_web("RUS", "ASFRstand")

# Read a locally downloaded file
df = pyhmfd.read_hmd("/path/to/Mx_1x1.txt")
df = pyhmfd.read_hfd("/path/to/asfrRR.txt", item="asfrRR")

Utility functions

# List available countries
pyhmfd.get_hmd_countries()         # ['AUS', 'AUT', ..., 'USA']
pyhmfd.get_hfd_countries()         # DataFrame with country names and codes
pyhmfd.get_hfc_countries()         # list of codes
pyhmfd.get_jmd_prefectures()       # dict: name → 2-digit code
pyhmfd.get_chmd_provinces()        # ['alb', 'bco', 'can', ...]

# List available data items per country
pyhmfd.get_hmd_items("USA")        # DataFrame: item, description, url
pyhmfd.get_hfd_items("USA")        # DataFrame: item, description, url

# Last-update date for an HFD country
pyhmfd.get_hfd_date("USA")         # '20260323' (date of last update)

Output format

All functions return a pandas.DataFrame. When fixup=True (default):

  • Age column is Int64 (nullable integer).
  • OpenInterval boolean column marks the terminal open age group (e.g. 110+).
  • Year and Cohort are Int64.
  • Rate and count columns are float64.
  • Missing values coded as '.' in source files become NaN.

Running tests

pytest

License

GPL-2.0, following the original R package.

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

pyhmfd-0.1.2.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

pyhmfd-0.1.2-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file pyhmfd-0.1.2.tar.gz.

File metadata

  • Download URL: pyhmfd-0.1.2.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pyhmfd-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6eb34373bd3e7a7f04596c0d4e972f543f2fefb4329cca11d4701a7135f94142
MD5 570acdd80bc48c044f4f9d442c141dcb
BLAKE2b-256 b73624fb831ec752f0e99a7b5b8daab4279e08c616c22e7057ed9a77e4fd8c73

See more details on using hashes here.

File details

Details for the file pyhmfd-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pyhmfd-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pyhmfd-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5dc3ff5ebdf6eb9d0978fa219b6bb2e34504fa43a606ea583714c3fe46ffc6a0
MD5 2598a670ca1d780c55c6b8f6065ff62b
BLAKE2b-256 b54e29e770e0243605bbce3266cebef29aa0911789024165d0695fadf583ae0f

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