Skip to content

pycensus.elections

Shared plumbing for election-result loaders (Spain, Andorra, Germany).

Column convention (<scope> = national | state | municipal): elec_<scope>_<year>_total_votes (valid votes incl. blank, count), ..._turnout_rate (votes cast / electors, only when electors are known), ..._left_share, ..._right_share, ..._other_share (sum to 1; blank votes and unlisted parties count as other) and ..._party_<slug>_share (share of valid votes) for every party above min_share of the loaded area. Also kept (counts, needed to re-aggregate shares exactly): ..._electors, ..._votes_cast, ..._left_votes, ..._right_votes, ..._party_<slug>_votes.

Bloc files (schema/*_blocs.json): {"parties": {KEY: {"bloc": "left|right|other", "uncertain": bool, "note": str}}, "default": "other"}.

H3: polygons -> res-8 cells is AREA-weighted (no fine population surface is available for all three countries): each polygon is covered with a finer H3 grid (res 8..11 chosen so it has >= ~30 sub-cells), sub-cells are mapped to their res-8 parent and the polygon's counts are split in proportion to the sub-cells in each parent. Counts are summed in the cell, shares are recomputed from the summed counts.

slugify(s)

Lower-case ASCII slug of a name (_ between words).

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 of an election.

Parameters:

Name Type Description Default
path

JSON file.

required

Returns:

Type Description
dict

The file's dict.

bloc_of(blocs, key)

Bloc (left / right / other) of a party.

Parameters:

Name Type Description Default
blocs dict

Dict from :func:load_blocs.

required
key str

Party key.

required

Returns:

Type Description
str

The bloc name.

build_frame(votes, totals, blocs, scope, year, min_share=0.03, keep_parties=None)

votes: long [GEOID, party, votes] (party = bloc-file key, candidacies only); totals: [GEOID, blank, cast, electors] (cast = valid + null votes; electors NaN/0 if unknown). Returns wide frame keyed by GEOID (see module doc).

Parameters:

Name Type Description Default
votes DataFrame

Long votes table.

required
totals DataFrame

Totals table.

required
blocs dict

Dict from :func:load_blocs.

required
scope str

"national", "state" or "municipal".

required
year int

Election year.

required
min_share float

Parties above this share of the area get their own columns.

0.03
keep_parties Optional[Iterable[str]]

Fixed list of parties.

None

order_columns(df)

Key columns (GEOID, h3_cell) first.

Parameters:

Name Type Description Default
df DataFrame

Table.

required

Returns:

Type Description
DataFrame

The reordered table.

h3_weights(gdf, res=8, id_col='GEOID')

Polygon -> res cell area weights: DataFrame [id_col, h3_cell, w] (w sums to 1 per polygon).

Parameters:

Name Type Description Default
gdf

Polygons.

required
res int

H3 resolution.

8
id_col str

Id column.

'GEOID'

to_h3(df, weights, scope, year, id_col='GEOID')

Distribute a frame from :func:build_frame over H3 cells with weights and recompute rates/shares from summed counts.

Parameters:

Name Type Description Default
df DataFrame

Frame from :func:build_frame.

required
weights DataFrame

Weights from :func:h3_weights.

required
scope str

Election scope.

required
year int

Election year.

required
id_col str

Id column.

'GEOID'