Skip to main content

There's a bit of an air of mystery around this project...

Project description

Easy access to (US 2020, for now) election statistics.

How to use

import pandas as pd
from elections import President2020TimeSeries, Races2020, Election2020RawJson

President's race stats

s = President2020TimeSeries()
len(s)
51

s is a dictionary-like interface to the presidential race.

It's keys are the states

print(*s)
alabama alaska arizona arkansas california colorado connecticut delaware district-of-columbia florida georgia hawaii idaho illinois indiana iowa kansas kentucky louisiana maine maryland massachusetts michigan minnesota mississippi missouri montana nebraska nevada new-hampshire new-jersey new-mexico new-york north-carolina north-dakota ohio oklahoma oregon pennsylvania rhode-island south-carolina south-dakota tennessee texas utah vermont virginia washington west-virginia wisconsin wyoming

It's values are dataframes containing the stats.

state = 'georgia'
df = s[state]
df
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
votes eevp eevp_source trumpd bidenj
timestamp
2020-11-04T09:23:03Z 0 0 edison 0.000 0.000
2020-11-04T00:14:11Z 408 0 edison 0.674 0.326
2020-11-04T00:15:51Z 127106 2 edison 0.370 0.618
2020-11-04T00:19:55Z 173638 3 edison 0.431 0.557
2020-11-04T00:21:57Z 174006 3 edison 0.432 0.557
... ... ... ... ... ...
2020-11-06T23:14:40Z 4969860 99 edison 0.493 0.494
2020-11-06T23:17:43Z 4969869 99 edison 0.493 0.494
2020-11-06T23:36:39Z 4969873 99 edison 0.493 0.494
2020-11-06T23:41:44Z 4969880 99 edison 0.493 0.494
2020-11-06T23:45:40Z 4970093 99 edison 0.493 0.494

456 rows × 5 columns

df['bidenj'].plot(figsize=(16, 6), grid=True, title=state);

png

Other races

But that's not the only race going on here.

s = Races2020()
len(s)
51
data = s['new-york']   # by the way, you can tab-complete this if you're working in a jupyter notebook
print(type(data))
print(f"{len(data)} items... Here are the first 5:")
list(data)[:5]
<class 'py2store.base.Store'>
242 items... Here are the first 5:





['president-general-2020-11-03',
 'house-general-district-001-2020-11-03',
 'house-general-district-002-2020-11-03',
 'house-general-district-003-2020-11-03',
 'house-general-district-004-2020-11-03']

So we see that now, instead of just getting the president's race, we get... 242 races (one of which is the president's race).

What you need to know, as well, is that President2020TimeSeries just gave you one of the many datas available for the race (the 'timeseries' one), extracted and formated for your convenience, since it's probably the main information you're here for.

But there are other associated (raw) datas you may or may not be interested in. Here's what you got:

data['president-general-2020-11-03'].keys()  # you can tab complete here as well (you're welcome!)
dict_keys(['race_id', 'race_slug', 'url', 'state_page_url', 'ap_polls_page', 'edison_exit_polls_page', 'race_type', 'election_type', 'election_date', 'runoff', 'race_name', 'office', 'officeid', 'race_rating', 'seat', 'seat_name', 'state_id', 'state_slug', 'state_name', 'state_nyt_abbrev', 'state_shape', 'party_id', 'uncontested', 'report', 'result', 'result_source', 'gain', 'lost_seat', 'votes', 'electoral_votes', 'absentee_votes', 'absentee_counties', 'absentee_count_progress', 'absentee_outstanding', 'absentee_max_ballots', 'provisional_outstanding', 'provisional_count_progress', 'poll_display', 'poll_countdown_display', 'poll_waiting_display', 'poll_time', 'poll_time_short', 'precincts_reporting', 'precincts_total', 'reporting_display', 'reporting_value', 'eevp', 'tot_exp_vote', 'eevp_source', 'eevp_value', 'eevp_display', 'county_data_source', 'incumbent_party', 'no_forecast', 'last_updated', 'candidates', 'has_incumbent', 'leader_margin_value', 'leader_margin_votes', 'leader_margin_display', 'leader_margin_name_display', 'leader_party_id', 'counties', 'votes2016', 'margin2016', 'clinton2016', 'trump2016', 'votes2012', 'margin2012', 'expectations_text', 'expectations_text_short', 'absentee_ballot_deadline', 'absentee_postmark_deadline', 'update_sentences', 'race_diff', 'winnerCalledTimestamp', 'timeseries'])
t = data['president-general-2020-11-03']
print(t['trump2016'], t['clinton2016'])
2819534 4556124

Election2020RawJson

But if you want even more, and even more raw than the above, we can give that to you.

With Election2020RawJson you get access to the original full json.

raw_jsons = Election2020RawJson()
json_data = raw_jsons['california']
json_data.keys()
dict_keys(['data', 'meta'])
json_data['meta']
{'version': 10403,
 'track': '2020-11-03',
 'timestamp': '2020-11-06T23:52:57.623Z'}
json_data['data'].keys()
dict_keys(['races', 'party_control', 'liveUpdates'])
pd.DataFrame(json_data['data']['party_control']).set_index('race_type').T
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
race_type house president senate
state_id
needed_for_control 218 270 50
total 435 538 100
no_election {} {} {'democrat': 35, 'republican': 30, 'other': 0}
winner False False False
parties {'democrat': {'party_id': 'democrat', 'name_di... {'democrat': {'party_id': 'democrat', 'name_di... {'democrat': {'party_id': 'democrat', 'name_di...
winnerCalledTimestamp None None None
pd.DataFrame(json_data['data']['liveUpdates'])
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
id author author_title_or_location text link_url link_text linked_state_1 linked_state_2 linked_state_3 image_url ... call_type race_id winner party_id candidate_last_name candidate_name_display candidate_id race_call_party_winner state_name link
0 333 Nate Cohn in New York New ballots from Clark County (that’s Las Vega... NV ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 332 Nate Cohn in New York The latest Arizona ballot releases aren’t look... AZ ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 331 Nick Corasaniti in Philadelphia There are still 102,000 mail ballots to be cou... PA ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 330 Dave Philipps in Las Vegas Biden nets 2,520 votes in the Las Vegas area, ... NV ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 329 Katie Glueck in Wilmington, Del. I’m told Biden spent the day watching election... ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
444 5 Nate Cohn in New York Tonight, the needle will be back — sort of. We... https://www.nytimes.com/2020/11/02/upshot/need... Learn more about the needle ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
445 4 Sheera Frenkel in Silicon Valley Times tech reporters will be monitoring for mi... ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
446 3 Michael Barbaro in New York From 4 p.m. to 8 p.m. Eastern time, we’ll be t... https://nytimes.com/thedaily Listen here ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
447 2 Trip Gabriel in Butler County, Pa. A look at Trump and the G.O.P.’s closing strat... https://www.nytimes.com/2020/11/02/us/politics... ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
448 1 Shane Goldmacher in New Hope, Pa. Here’s our recap of the final day of campaigni... https://www.nytimes.com/2020/11/02/us/politics... ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

449 rows × 31 columns




          

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elections-0.0.2.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

elections-0.0.2-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page