Skip to main content

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.

Metadata

Release files for pyhmfd 0.1.4

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

Source distribution (sdist)

Source distribution for pyhmfd 0.1.4
File Size Uploaded
pyhmfd-0.1.4.tar.gz 13.9 kB Details

Built distribution (wheel)

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

Total release size: 31.0 kB

Release files / pyhmfd-0.1.4.tar.gz

Download URL pyhmfd-0.1.4.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
065687e08e1a610517209d52dbede1bff3a3f5697843280e2dab24ffafef2559
BLAKE2b-256 checksum
How to use checksums
807dd5a3dd10e7481ec3c5403807ddfdee9ffe8f2c3c304d797be23022ccdc8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / pyhmfd-0.1.4-py3-none-any.whl

Download URL pyhmfd-0.1.4-py3-none-any.whl
Size 17.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f1c84777ca7c602d6c1860d02ceecb9a5a077dd57b56148e676cbbe4ab59ca4
BLAKE2b-256 checksum
How to use checksums
89a5ea9eb07476c3d492950f0b45e02b1c227e98bf53fed0a45cdda108ac63d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

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