Skip to content

pycensus.census.countries.spain.ine

Spain Census 2021 (Censo de Población y Viviendas 2021) source.

Importing this module registers the gdf.ine GeoDataFrame accessor (see :mod:pycensus.accessors). Use :func:load to fetch data.

Technical aspects

Unlike US census data which has APIs, Spain's INE Census 2021 data must be downloaded as files from INE's website. This module provides tools to: 1. Load and process the data into a common-schema GeoDataFrame 2. Cache processed data for efficient repeated access 3. Download data to the cache directory

Source

https://www.ine.es/dyngs/INEbase/es/operacion.htm?c=Estadistica_C&cid=1254736176780

download_census_data(level='municipality', regions=None, data_dir=None, overwrite=False)

Force a fresh download of INE's Padron municipal table to the raw cache.

Parameters:

Name Type Description Default
level str

Accepted for interface parity; the Padron API returns every municipality in one request regardless of level.

'municipality'
regions Optional[Union[str, List[str]]]

Accepted for interface parity; not used to filter the download (the raw file always holds every municipality).

None
data_dir Optional[str]

Root directory for raw downloads.

None
overwrite bool

If True, delete the cached raw JSON file first so it is re-downloaded from INE.

False

Returns:

Type Description
str

Path to the raw cache file that was downloaded/refreshed.

Note

Only the Padron (population by sex) is implemented here. INE's Census 2021 (Censo de Poblacion y Viviendas) publishes households, housing, education, and employment data at municipality level too, via the same Tempus API under a different operation/table id, but that was not identified and verified against a real downloaded file in this rebuild -- see the module docstring.

load(aoi=None, regions=None, level='municipality', year='latest', api_key=None, cache_dir=None, cache=True, data_dir=None)

Load INE's Padron municipal population data as a common-schema GeoDataFrame.

Parameters:

Name Type Description Default
aoi Optional[GeoDataFrame]

Area of interest. Used to auto-detect regions if not given, and to clip the final result.

None
regions Optional[Union[str, List[str]]]

Region code(s) -- autonomous community code, province code, or 5-digit municipality code, matching level. Derived from aoi if omitted (aoi is then required).

None
level str

Standard geometry level name. Spain supports nation, autonomous_community, province, municipality, section directly; other standard levels fall back via :data:pycensus.spain.constants.LEVEL_FALLBACK (tract/ blockgroup -> section). district/block are not implemented (see :mod:pycensus.spain.geography). section population comes from a different, per-province INE source (2022 vintage) than municipality+ (2025 vintage) -- see :mod:pycensus.spain.api module docstring.

'municipality'
year Union[int, str]

Vintage year, or "latest". INE's Padron API always returns its single latest published year (2025 as of this module's implementation) -- year is accepted for interface parity but not otherwise used to select a vintage.

'latest'
api_key Optional[str]

Not used -- INE's Padron Tempus API requires no API key.

None
cache_dir Optional[str]

Root of pyCensus' parquet cache. Defaults to :func:pycensus.cache.default_cache_dir.

None
cache bool

If False, bypass the parquet cache entirely.

True
data_dir Optional[str]

Root directory for raw INE/GISCO downloads (separate from cache_dir). Defaults to :data:pycensus.spain.constants.DEFAULT_DATA_DIR.

None

Returns:

Type Description
GeoDataFrame

GeoDataFrame with GEOID, geometry, and one ine_-prefixed

GeoDataFrame

column per logical column in :data:pycensus.spain.ine.schema.SCHEMA.

Raises:

Type Description
ValueError

If neither regions nor aoi is given.

NotImplementedError

If level resolves to a level without a real boundary source (see :mod:pycensus.spain.geography).