Skip to content

pycensus.census.countries.usa.elections

US election results (MEDSL) -- see loader's module docstring for exactly what's implemented and why the rest is a documented gap.

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

"president" or "senate" (see COUNTY_AVAILABLE/ STATE_AVAILABLE for exactly which office/year/level combinations are implemented -- House and most non-2024 county-level non-presidential years are genuine, documented gaps, not silently wrong output).

required
year int

Election year.

required
level str

"county" or "state" -- BOTH are genuinely native here (see module docstring): president/senate are state-decided contests with their own independently-published MEDSL state-level file, not merely a derived sum of the county one.

'county'
state_fips Optional[str]

Optional state FIPS filter (2-digit, e.g. "35"). Required (via state_abbr) for county-level president/2024 specifically, since that source has no combined file.

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 pycensus.cache.default_cache_dir().

None

Returns:

Type Description
DataFrame

DataFrame with (at least) GEOID (5-digit county FIPS if

DataFrame

level="county", 2-digit state FIPS if level="state"),

DataFrame

candidate, party, votes, year.

Raises:

Type Description
NotImplementedError

If (office, year) isn't implemented at the requested level.

ValueError

If level isn't "county"/"state", or county-level president/2024 is requested with no state_abbr/resolvable state_fips.