Skip to content

pycensus.electoral

Country-agnostic helpers for election-result loaders (counts -> shares, polygons <-> H3 res 8).

Counts convention (all additive, so they can be resampled): eligible, ballots (voters who cast a ballot), valid (valid votes), left, right, other (valid votes by bloc) and party_<slug> (valid votes per party / candidate). :func:finalize turns them into the project columns elec_<tag>_total_votes (= valid votes), _turnout_rate (= ballots / eligible), _left_share/_right_share/ _other_share and _party_<slug>_share (shares of valid votes, 0-1).

Resampling is AREA-WEIGHTED (documented choice, no population raster needed): a polygon's count is split among the H3 res-8 cells it overlaps in proportion to the intersection area / polygon area (mass conserving); cells -> target polygons the same way (cell count x intersection / cell area). Counts, never shares, are resampled; shares are recomputed afterwards.

slugify(s)

Lower-case slug of a name.

Parameters:

Name Type Description Default
s str

Any text.

required

Returns:

Type Description
str

The slug ("x" when empty).

load_blocs(path)

The party-to-bloc file.

Parameters:

Name Type Description Default
path Path

JSON file.

required

Returns:

Type Description
dict

The file's dict.

count_cols(df)

The count columns of a frame (base counts and party_*).

Parameters:

Name Type Description Default
df DataFrame

Counts table.

required

Returns:

Type Description
List[str]

Column names.

finalize(counts, tag, key, party_slugs)

Counts frame (key + count columns) -> project columns elec_<tag>_* (shares 0-1).

Parameters:

Name Type Description Default
counts DataFrame

Counts table.

required
tag str

Election tag of the output columns.

required
key str

Key column.

required
party_slugs Iterable[str]

Parties given their own columns.

required

cells_for(gdf, res=H3_RES)

H3 cells (h3_cell, hex polygons in EPSG:4326) covering gdf (any cell touching a polygon, via cell-centre cover + neighbours).

Parameters:

Name Type Description Default
gdf GeoDataFrame

Polygons.

required
res int

H3 resolution.

H3_RES

polygons_to_h3(gdf, cols, res=H3_RES)

Area-weighted, mass-conserving split of polygon counts cols onto H3 cells. Returns h3_cell + cols.

Parameters:

Name Type Description Default
gdf GeoDataFrame

Polygons with counts.

required
cols List[str]

Count columns.

required
res int

H3 resolution.

H3_RES

h3_to_polygons(h3df, target, key, cols)

Area-weighted split of H3-cell counts cols onto target polygons (key column). Mass is conserved for cells fully covered.

Parameters:

Name Type Description Default
h3df DataFrame

Counts per H3 cell.

required
target GeoDataFrame

Target polygons.

required
key str

Target id column.

required
cols List[str]

Count columns.

required

points_to_h3(points, cols, res=H3_RES, lat='lat', lng='lng')

Counts located at a point (e.g. polling station) -> the H3 cell containing it.

Parameters:

Name Type Description Default
points DataFrame

Table with coordinates.

required
cols List[str]

Count columns.

required
res int

H3 resolution.

H3_RES
lat str

Latitude column.

'lat'
lng str

Longitude column.

'lng'

Returns:

Type Description
DataFrame

Counts per H3 cell.