-
Notifications
You must be signed in to change notification settings - Fork 0
/
oh2020.py
600 lines (470 loc) · 21.7 KB
/
oh2020.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import pandas as pd
import electioncleaner as EC
DataFrame = pd.core.frame.DataFrame
Series = pd.core.series.Series
def load_nonlocal_file() -> DataFrame:
file = 'raw/statewideresultsbyprecinct.xlsx'
print(f'*Reading file {file}...')
data = pd.read_excel(file, sheet_name="Master", header=[0, 1], skiprows=[2, 3])
# Remove WRITEIN from this file as they contain nothing
data = data[[column for column in data.columns if '(WI)' not in column[1]]]
STR = ('November 3, 2020 General Election Official Canvass\nAll Member of the State Board of '
'Education, Justice of the Supreme Court and Judge of the Court of Appeals races are '
'non-partisan.\n*Write-in candidates will be displayed with a (WI) designation and not '
'party affiliation.\n*Precinct-level data is not available for write-in candidates. To '
'view results for write-in candidates, download the Summary-level spreadsheet.')
variables = [(STR, 'County Name'), (STR, 'Precinct Name'), (STR, 'Precinct Code'),
(STR, 'Region Name'), (STR, 'Media Market'), (STR, 'Registered Voters'),
(STR, 'Ballots Counted'), (STR, 'Official Voter Turnout')]
data = pd.melt(data, id_vars=variables)
data = data.rename(columns={
(STR, 'County Name'): 'County',
(STR, 'Precinct Name'): 'Precinct',
(STR, 'Precinct Code'): 'Precinct Code',
(STR, 'Region Name'): 'Region Name',
(STR, 'Media Market'): 'Media Market',
(STR, 'Registered Voters'): 'Registered Voters',
(STR, 'Ballots Counted'): 'Ballots Counted',
(STR, 'Official Voter Turnout'): 'Official Voter Turnout',
'variable_0': 'Office',
'variable_1': 'Candidate/Party',
'value': 'Votes',
})
# Discard trivial 0 votes (and non-trivial ones).
# Ohio lists 0 votes for candidates that did not run in a particular district, which if left
# untreated, yields millions of records records; which is too much
# We will remove offices that indicate county name where they do not correspond to the
# actual county.
data = data[((data['Office'] == 'President and Vice-President\n') |
# (data['Office'].str.contains('County')) |
(data['Votes'] > 0))].reset_index(drop=True)
# Fix Registered Voters and Ballots Counted so they are proper records.
data = data[['County', 'Precinct', 'Registered Voters', 'Ballots Counted',
'Office', 'Candidate/Party', 'Votes']]
data1 = data[['County', 'Precinct', 'Office', 'Candidate/Party', 'Votes']]
data2 = data[['County', 'Precinct', 'Registered Voters',
'Ballots Counted']].drop_duplicates()
data2 = pd.melt(data2, id_vars=['County', 'Precinct'],
var_name='Office', value_name='Votes')
data2['Candidate/Party'] = ''
data2 = data2[['County', 'Precinct', 'Office', 'Candidate/Party', 'Votes']]
data = data1.append(data2)
print(f'Read file {file}.')
return data
def load_local_file() -> DataFrame:
file = 'raw/CountyRaceResultsByPrecinct.xlsx'
print(f'*Reading file {file}...')
# This line alone took 4 minutes in my machine. You better have a good reason to run this.
data = pd.read_excel(file, sheet_name="Master", header=[0, 1], skiprows=[2, 3])
STR = ('November 3, 2020 General Election Official Canvass\nAll Judge of the Court of Common '
'Pleas and Judge of the County Court races are non-partisan.\n*Write-in candidates will '
'be displayed with a (WI) designation and not party affiliation.\n*Precinct-level data '
'is not available for write-in candidates. To view results for write-in candidates, '
'download the Summary-level spreadsheet.')
variables = [(STR, 'County Name'), (STR, 'Precinct Name'), (STR, 'Precinct Code'),
(STR, 'Region Name'), (STR, 'Media Market'), (STR, 'Registered Voters'),
(STR, 'Ballots Counted'), (STR, 'Official Voter Turnout')]
# Remove WRITEIN from this file as they contain nothing
data = data[[column for column in data.columns if '(WI)' not in column[1]]]
data = pd.melt(data, id_vars=variables)
data = data.rename(columns={
(STR, 'County Name'): 'County',
(STR, 'Precinct Name'): 'Precinct',
(STR, 'Precinct Code'): 'Precinct Code',
(STR, 'Region Name'): 'Region Name',
(STR, 'Media Market'): 'Media Market',
(STR, 'Registered Voters'): 'Registered Voters',
(STR, 'Ballots Counted'): 'Ballots Counted',
(STR, 'Official Voter Turnout'): 'Official Voter Turnout',
'variable_0': 'Office',
'variable_1': 'Candidate/Party',
'value': 'Votes',
})
data = data[['County', 'Precinct', 'Office', 'Candidate/Party', 'Votes']]
# We will remove offices that indicate county name where they do not correspond to the
# actual county.
data['office_county'] = data['Office'].str.extract('(.* )([A-Za-z]+)( County.*)')[1]
data = data[((data['office_county'].isna()) |
(data['office_county'] == data['County']))].reset_index(drop=True)
data = data.drop(labels='office_county', axis=1)
data = EC.adapt_column(data, 'Office',
'(?P<name1>.*) - (?P<County>.*) County\n?(?P<name2>.+)',
'{name1}: {name2}')
data = EC.adapt_column(data, 'Office',
'(?P<name1>.*) - (?P<County>.*) County\n',
'{name1}')
print(f'Read file {file}.')
return data
def load_nonlocal_writein_file() -> DataFrame:
# We use the county file to recover aggregate writein results
file = 'raw/statewideresultsbycounty.xlsx'
print(f'*Reading file {file}...')
data = pd.read_excel(file, sheet_name="Master", header=[0, 1], skiprows=[2, 3])
# Remove WRITEIN from this file as they contain nothing
data = data[[column for column in data.columns if '(WI)' in column[1] or 'Nov' in column[0]]]
STR = ('November 3, 2020 General Election Official Canvass\nAll Member of the State Board of '
'Education, Justice of the Supreme Court and Judge of the Court of Appeals races are '
'non-partisan.\n*Write-in candidates will be displayed with a (WI) designation and not '
'party affiliation.')
variables = [(STR, 'County Name'), (STR, 'Region Name'), (STR, 'Media Market'),
(STR, 'Registered Voters'), (STR, 'Ballots Counted'),
(STR, 'Official Voter Turnout')]
data = pd.melt(data, id_vars=variables)
data = data.rename(columns={
(STR, 'County Name'): 'County',
(STR, 'Precinct Name'): 'Precinct',
(STR, 'Precinct Code'): 'Precinct Code',
(STR, 'Region Name'): 'Region Name',
(STR, 'Media Market'): 'Media Market',
(STR, 'Registered Voters'): 'Registered Voters',
(STR, 'Ballots Counted'): 'Ballots Counted',
(STR, 'Official Voter Turnout'): 'Official Voter Turnout',
'variable_0': 'Office',
'variable_1': 'Candidate',
'value': 'Votes',
})
data = data[['County', 'Office', 'Votes']]
data = data.groupby(['County', 'Office']).sum().reset_index()
data['Candidate/Party'] = 'WRITEIN (NP)'
data['Precinct'] = 'COUNTY FLOATING'
print(f'Read file {file}.')
return data
def load_local_writein_file() -> DataFrame:
# We use the county file to recover aggregate writein results
file = 'raw/countyracebycounty.xlsx'
print(f'*Reading file {file}...')
data = pd.read_excel(file, sheet_name="Master", header=[0, 1], skiprows=[2, 3])
# Remove WRITEIN from this file as they contain nothing
data = data[[column for column in data.columns if '(WI)' in column[1] or 'Nov' in column[0]]]
STR = ('November 3, 2020 General Election Official Canvass\nAll Judge of the Court of Common '
'Pleas and Judge of the County Court races are non-partisan.\n*Write-in candidates will '
'be displayed with a (WI) designation and not party affiliation.')
variables = [(STR, 'County Name'), (STR, 'Region Name'), (STR, 'Media Market'),
(STR, 'Registered Voters'), (STR, 'Ballots Counted'),
(STR, 'Official Voter Turnout')]
data = pd.melt(data, id_vars=variables)
data = data.rename(columns={
(STR, 'County Name'): 'County',
(STR, 'Precinct Name'): 'Precinct',
(STR, 'Precinct Code'): 'Precinct Code',
(STR, 'Region Name'): 'Region Name',
(STR, 'Media Market'): 'Media Market',
(STR, 'Registered Voters'): 'Registered Voters',
(STR, 'Ballots Counted'): 'Ballots Counted',
(STR, 'Official Voter Turnout'): 'Official Voter Turnout',
'variable_0': 'Office',
'variable_1': 'Candidate',
'value': 'Votes',
})
data = data[['County', 'Office', 'Votes']]
data = data.groupby(['County', 'Office']).sum().reset_index()
data['Candidate/Party'] = 'WRITEIN (NP)'
data['Precinct'] = 'COUNTY FLOATING'
# We will remove offices that indicate county name where they do not correspond to the
# actual county.
data['office_county'] = data['Office'].str.extract('(.* )([A-Za-z]+)( County.*)')[1]
data = data[((data['office_county'].isna()) |
(data['office_county'] == data['County']))].reset_index(drop=True)
data = data.drop(labels='office_county', axis=1)
data = EC.adapt_column(data, 'Office',
'(?P<name1>.*) - (?P<County>.*) County\n?(?P<name2>.+)',
'{name1}: {name2}')
data = EC.adapt_column(data, 'Office',
'(?P<name1>.*) - (?P<County>.*) County\n',
'{name1}')
data = data[['County', 'Precinct', 'Office', 'Candidate/Party', 'Votes']]
print(f'Read file {file}.')
return data
def load_all_data(prepare_pickle=True) -> DataFrame:
if prepare_pickle:
# WARNING! SLOW!!!!
data = pd.DataFrame()
for func in [load_nonlocal_file,
load_nonlocal_writein_file,
load_local_file,
load_local_writein_file]:
file_data = func()
data = data.append(file_data).reset_index(drop=True)
data.to_pickle('raw_OH20.pkl')
data = pd.read_pickle('raw_OH20.pkl')
return data
def make_state(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `state`...')
# State is Ohio by definition
# This has to be performed first to allow to search county and jurisdiction fips later
data = EC.state.add_state_codes(data, state='Ohio')
print('Parsed OH20 `state`.')
return data
def make_precinct(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `precinct`...')
# Data is pulled straight from `Precinct`.
data['precinct'] = data['Precinct'].str.strip().str.upper()
print('Parsed OH20 `precinct`.')
return data
def make_office(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `office`...')
# Data is pulled from `Office`
data['temp_office'] = data['Office'].str.upper().astype(str).str.replace('\n', '')
# Standardize names
standard_names = {
r'PRESIDENT.*': 'US PRESIDENT',
r'REPRESENTATIVE TO CONGRESS': 'US HOUSE',
r'STATE SENATOR': 'STATE SENATE',
r'STATE REPRESENTATIVE': 'STATE HOUSE',
r'SHERIFF': 'COUNTY SHERIFF',
r'CORONER': 'COUNTY CORONER',
r'COUNTY COUNCIL DISTRICT': 'COUNTY COUNCIL - DISTRICT', # Helps with regex later
}
data['temp_office'] = data['temp_office'].replace(standard_names, regex=True)
# For a lot of of offices there is a term commencing/ending date. It used to be separated by a
# newline, but that was removed earlier. Add a colon+space to separate for now
data['temp_office'] = data['temp_office'].replace({
'(?<!: )TERM COMMENCING': ': TERM COMMENCING',
'(?<!: )UNEXPIRED TERM ENDING': ': UNEXPIRED TERM ENDING',
}, regex=True)
print('Parsed OH20 `temp_office`.')
return data
def make_party_detailed(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `party_detailed...`')
# Data is pulled from `Candidate/Party`, upper-cased, and standardizing party names
data['party_detailed'] = data['Candidate/Party']
# Standardize names
standard_names = {
'(D)': 'DEMOCRAT',
'(R)': 'REPUBLICAN',
'(L)': 'LIBERTARIAN',
'(NP)': 'NONPARTISAN'
}
def build_party(candidateparty: str) -> str:
for (abbreviation, party) in standard_names.items():
if abbreviation in candidateparty:
return party
return 'NONPARTISAN'
data['party_detailed'] = data['party_detailed'].apply(build_party)
# Make writeins be empty
data['party_detailed'] = data['party_detailed'].mask(
data['Candidate/Party'] == 'WRITEIN (NP)', '')
print('Parsed OH20 `party_detailed`.')
return data
def make_party_simplified(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `party_simplified...`')
# We can use the details from the recently parsed OH20 party_detailed for this.
data['party_simplified'] = data['party_detailed']
print('Parsed OH20 `party_simplified`.')
return data
def make_mode(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `mode`...')
# All vote totals are TOTAL
data['mode'] = 'TOTAL'
print('Parsed OH20 `mode`.')
return data
def make_votes(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `votes...`')
# Data is pulled straight from `Votes`
data['votes'] = pd.to_numeric(data['Votes'], errors='raise')
print('Parsed OH20 `votes`.')
return data
def make_county_name(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `county_name`...')
# Data is pulled straight from `County` and upper cased
data['county_name'] = data['County'].str.upper()
print('Parsed OH20 `county_name`.')
return data
def make_county_fips(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `county_fips`...')
# Use recently obtained `county_name` field and list of county fips codes
fips = pd.read_csv(r"..\..\help-files\county-fips-codes.csv")
fips['state'] = fips['state'].str.upper()
data = data.join(fips.set_index(['state', 'county_name']), on=['state', 'county_name'],
how="left")
data['county_fips'] = data['county_fips'].astype(int, errors='raise') # Force int
print('Parsed OH20 `county_fips`.')
return data
def make_jurisdiction_name(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `jurisdiction_name`...')
# `jurisdiction_name` is the same as `county_name` for Ohio, so use that
data['jurisdiction_name'] = data['county_name']
print('Parsed OH20 `jurisdiction_name`.')
return data
def make_jurisdiction_fips(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `jurisdiction_fips`...')
# `jurisdiction_fips` is the same as `county_fips` for Ohio, so use that
data['jurisdiction_fips'] = data['county_fips']
print('Parsed OH20 `jurisdiction_fips`.')
return data
def make_candidate(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `candidate...`')
# Data is pulled from Candidate and uppercased
data['candidate'] = data['Candidate/Party'].str.upper()
# First, remove any extraneous whitespace/characters
data['candidate'] = data['candidate'].str.strip().replace({
r' ( )+': ' ',
r'\.': '',
}, regex=True)
# Now, trim party affiliation from the end
data['candidate'] = data['candidate'].replace({
r' \(D\)': '',
r' \(R\)': '',
r' \(L\)': '',
r' \(NP\)': '',
}, regex=True)
# For presidential candidates, we just record the president's name
data['candidate'] = data['candidate'].replace({
'JOSEPH R BIDEN AND KAMALA D HARRIS': 'JOSEPH R BIDEN',
'JO JORGENSEN AND SPIKE COHEN': 'JO JORGENSEN',
'HOWIE HAWKINS AND ANGELA WALKER': 'HOWIE HAWKINS',
'DONALD J TRUMP AND MICHAEL R PENCE': 'DONALD J TRUMP',
})
print('Parsed OH20 `candidate`...')
return data
def make_district(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `district`...')
# Extract district information from `temp_office`
regex = '(?P<temp_office1>.*) - DISTRICT (?P<district>[0-9]+)(?P<temp_office2>.*)'
data = EC.split_column(data, 'temp_office',
regex,
maintaining_columns=['temp_office'], empty_value='')
data = EC.merge_columns(data, 'temp_office_merged', '{temp_office1}{temp_office2}')
# At this point we are done modifying office
data['office'] = EC.left_merge_series([data['temp_office'], data['temp_office_merged']], {''})
data['district'] = EC.district.mark_statewide_districts(
data['district'], data['office'], [
'PRESIDENT',
'JUSTICE OF THE SUPREME COURT',
])
data['district'] = EC.district.fix_numerical_districts(data['district'])
print('Parsed OH20 `office` (3/3).')
print('Parsed OH20 `district`.')
return data
def make_magnitude(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `magnitude`...')
# Magnitude is 1 except for statistics
data['magnitude'] = EC.iif(data['office'], lambda series: series.isin({
'BALLOTS COUNTED',
'REGISTERED VOTERS',
}), 0, 1)
print('Parsed OH20 `magnitude`.')
return data
def make_dataverse(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `dataverse`...')
data['dataverse'] = EC.dataverse.parse_dataverse_from_office(
data['office'],
state={
'JUSTICE OF THE SUPREME COURT',
'JUDGE OF THE COURT OF APPEALS',
'MEMBER OF THE STATE BOARD OF EDUCATION',
'STATE HOUSE',
'STATE SENATE',
'CLERK OF THE COURT OF COMMON PLEAS',
},
empty={
'REGISTERED VOTERS',
'BALLOTS COUNTED',
})
# Manually do this case because there are so many
data['dataverse'] = data['dataverse'].mask(
data['office'].str.contains('JUDGE OF THE COURT OF COMMON PLEAS'),
'STATE')
print('Parsed OH20 `dataverse`.')
return data
def make_year(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `year`...')
# Year is 2020 by definition
data['year'] = 2020
print('Parsed OH20 `year`.')
return data
def make_stage(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `stage`...')
# Stage is consistently general for current data
data['stage'] = 'GEN'
print('Parsed OH20 `stage`.')
return data
def make_special(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `special`...')
# Unexpired elections are special. Those are the only special elections.
data['special'] = EC.series_r_bool(data['office'].str.contains('UNEXPIRED'))
print('Parsed OH20 `special`.')
return data
def make_writein(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `writein`...')
# The Ohio data indicates this in the candidate field
data['writein'] = EC.series_r_bool(data['candidate'] == 'WRITEIN')
print('Parsed OH20 `writein`.')
return data
def make_state_po(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `state_po`...')
# Already parsed
print('Parsed OH20 `state_po`.')
return data
def make_state_fips(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `state_fips`...')
# Already parsed
print('Parsed OH20 `state_fips`.')
return data
def make_state_cen(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `state_cen`...')
# Already parsed
print('Parsed OH20 `state_cen`.')
return data
def make_state_ic(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `state_ic`...')
# Already parsed
print('Parsed OH20 `state_ic`.')
return data
def make_date(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `date...`')
# Ohio had one date for all elections
data['date'] = '2020-11-03'
print('*Parsed OH20 `date`.')
return data
def make_readme_check(data: DataFrame) -> DataFrame:
print('*Parsing OH20 `readme_check...`')
data['readme_check'] = EC.series_r_bool(
# Remarks about vote drops
(data['office'] != 'US PRESIDENT') |
# Writeins are county aggregates
(data['candidate'] == 'WRITEIN')
)
print('Parsed OH20 `readme_check`.')
return data
if __name__ == '__main__':
print("Parsing raw data for Ohio.")
raw_data = load_all_data(prepare_pickle=True)
print("Parsed OH20 raw data for Ohio.")
EC.check_original_dataset(
raw_data,
expected_columns={'County', 'Precinct', 'Office', 'Candidate/Party', 'Votes'},
county_column='County', expected_counties=88
)
data = raw_data.copy()
# Parse needed details for standard form
data = make_state(data)
data = make_precinct(data)
data = make_office(data)
data = make_party_detailed(data)
data = make_party_simplified(data)
data = make_mode(data)
data = make_votes(data)
data = make_county_name(data)
data = make_county_fips(data)
data = make_jurisdiction_name(data)
data = make_jurisdiction_fips(data)
data = make_candidate(data)
data = make_district(data)
data = make_magnitude(data)
data = make_dataverse(data)
data = make_year(data)
data = make_stage(data)
data = make_special(data)
data = make_writein(data)
data = make_state_po(data)
data = make_state_fips(data)
data = make_state_cen(data)
data = make_state_ic(data)
data = make_date(data)
data = make_readme_check(data)
data = EC.select_cleaned_dataset_columns(data, False)
data = EC.sort_cleaned_dataset(data)
EC.check_cleaned_dataset(data, expected_counties=88, expected_jurisdictions=88)
EC.inspect_cleaned_dataset(data)
EC.save_cleaned_dataset(data, '2020-oh-precinct-general.csv')