Skip to content

Getting Started

This guide will help you install popmex and get the population of Mexico at municipal and hexagon resolution in a few minutes.

Prerequisites

Python 3.10 or higher. pandas, geopandas, pyarrow and pooch are installed automatically.

Installation

pip install git+https://github.com/fdzul/popmex-py
pip install git+https://github.com/fdzul/rgeomex-py      # for extract_pop()

1. The datasets

import popmex
popmex.datasets()
Dataset Content Where
pop2000, pop2010, pop2020 Census population by municipality, sex and age group (INEGI ITER) in the package
conapop Population by municipality, sex and five-year age group, 1990-2040 (CONAPO) downloaded the first time
hdx_pop2021, hdx_pop2022, hdx_pop2023 Population in 400 m H3 hexagons (Kontur / HDX) downloaded the first time
landscan Population raster of 1 km, one band per year 2000-2026 (LandScan) get_landscan(), downloaded the first time

The census tables come with the package. conapop (8.5 MB) and the hexagons (23-37 MB each) are downloaded the first time they are used, verified and kept in a local cache (popmex.cache_dir()); set POPMEX_DATA_DIR to use another folder.

2. Load a dataset

pop = popmex.load("pop2020")                   # all the municipalities
yuc = popmex.load("pop2020", cve_edo="31")     # one state
con = popmex.load("conapop", cve_edo=["14", "31"])

The municipality key (CVEGEO; cvegeo in conapop) is text with its leading zero ("01001"), ready to join with surveillance data.

3. Hexagons of a city

gdl = popmex.extract_pop(year=2023, cve_edo="14",
                         locality=["Guadalajara", "Zapopan", "Tonalá", "Tlaquepaque"])

extract_pop() takes the boundaries of the localities from rgeomex (the names do not need to be exact: "Merida" finds "Mérida") and returns the hexagons that intersect them, with their population.

4. Rates

popmex.rate(cases=25, population=50_000)                  # 50 per 100,000
popmex.rate(df["casos"], df["pobtot"], per=10_000)        # columns

A population of 0 gives NaN instead of infinity.

5. LandScan

popmex.landscan_years()                                    # 2000 ... 2024, 2026
popmex.landscan_population(municipios, year=2020, id_col="CVEGEO")

It needs rasterio: pip install rasterio.