pycensus.census.countries.usa.elections.loader¶
US election results, from MIT Election Data and Science Lab (MEDSL).
MEDSL's primary distribution channel is Harvard Dataverse, but its
newer/updated datasets (e.g. "County Presidential Election Returns
2000-2024", doi:10.7910/DVN/VOQCHQ) are gated behind a Dataverse
"Guestbook" survey response that can't be satisfied by a plain scripted
request -- confirmed live 2026-09-29: GET
/api/access/datafile/<id> returns "You may not download this file
without the required Guestbook response for guestbookID 458." even with
no auth at all, and the same gate applies dataverse-wide. Getting past it
would need a real Harvard Dataverse account + API token that has
answered the guestbook once through the actual web UI -- not something
this module can or should fabricate.
Two genuinely NATIVE levels, not one (per Miguel's explicit
2026-09-29 direction: elections should be downloaded at their own real
native level(s), then resampled onto census geography -- not treated as
county-only): President and Senate are both fundamentally STATE-level
contests (winner-take-all electors / one statewide seat), and MEDSL
separately publishes real state-level totals for both, not merely a
derived sum of its own county file -- so level="state" is just as
native as level="county", from a different, independently-published
MEDSL file each time.
What's actually implemented, and why (all verified live 2026-09-29 against real, ungated GitHub URLs, no guessing):
- President, county level, 2000-2016:
github.com/MEDSL/ county-returns(countypres_2000-2016.csv), one combined CSV, real countyFIPS. - President, county level, 2024: NOT available as a convenient
county-level file anywhere ungated -- county numbers only exist inside
github.com/MEDSL/2024-elections-official'sindividual_states/ {state}24.zipfiles, at PRECINCT level (confirmed: Rhode Island's file has a realcounty_fipscolumn per precinct row). This module downloads one state's precinct zip on demand (fits this project's own per-state processing architecture) and aggregates it to county itself. - Senate, county level, 2024:
2024-elections-official's own2024-senate-county.csv-- already at county level. - President, STATE level, 1976-2016:
github.com/MEDSL/ constituency-returns(1976-2016-president.csv) -- a genuinely separate, independently-published state-level file (NOT the same file as the county one, and NOT summed from it). - Senate, STATE level, 1976-2018: same repo,
1976-2018-senate.csv. - President/Senate, STATE level, 2024:
2024-elections-official's own2024-president-state.csv/2024-senate-state.csv. - NOT implemented, genuine gaps (flagged here rather than silently producing partial-looking-complete data):
- House: no ungated source found at either level, for any year -- House is its own distinct electoral geography (congressional district, not county or state), a real, separate gap from "just missing years."
- Senate at COUNTY level for years other than 2024, and President at COUNTY level for 2024's full multi-state convenience (only per-state on demand): no ungated county-level source found for these specifically (state-level IS available for a wider year range, per above -- this gap is about county granularity only).
- 2026: no election has happened yet as of this module's writing (2026-09-29; the midterms are in November 2026) -- there is no data to fetch.
- Local/municipal races: out of scope (see the separately-documented decision to drop "local district" as a geography level entirely -- no national dataset for the districts themselves, let alone results).
Output columns are named {election_year}_{office}_... per
usa_study/prompt.txt ("Elections info should be {election_year}_
{election level or name}"), e.g. 2024_president_total_votes,
2016_president_democrat_share.
load(office, year, level='county', state_fips=None, state_abbr=None, cache_dir=None)
¶
Load election results for one office/year, at its real native level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
office
|
str
|
|
required |
year
|
int
|
Election year. |
required |
level
|
str
|
|
'county'
|
state_fips
|
Optional[str]
|
Optional state FIPS filter (2-digit, e.g. |
None
|
state_abbr
|
Optional[str]
|
Optional 2-letter state abbreviation filter/selector. |
None
|
cache_dir
|
Optional[str]
|
Cache root for downloaded source files. Defaults to
|
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with (at least) |
DataFrame
|
|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If |
ValueError
|
If |