Exemplo n.º 1
0
from open_elections.tools import StateDataFormat


def remove_invalid_vote_counts(dic: dict):
    value = dic['votes']
    if value in ['x', 'X', 'X ', ' ', 'X394', 'X.', 'X:', '"X"', '-', '`']:
        dic['votes'] = None


national_precinct_dataformat = StateDataFormat(
    row_cleaners=[remove_invalid_vote_counts])
Exemplo n.º 2
0
from open_elections.tools import StateDataFormat
import pandas as pd


def remove_invalid_vote_counts(dic: dict):
    value = dic['votes']
    if value in ['Write-ins', 'ESTES R', 'I']:
        dic['votes'] = None


def rename_vote_cols(df: pd.DataFrame) -> pd.DataFrame:
    if 'Unnamed: 6'.lower() in df.columns:
        return df.rename(columns={'Unnamed: 6'.lower(): 'votes'})
    else:
        return df


national_precinct_dataformat = StateDataFormat(
    df_transformers=[rename_vote_cols],
    row_cleaners=[remove_invalid_vote_counts])
Exemplo n.º 3
0
from open_elections.tools import StateDataFormat
import pandas as pd


# need to exclude 20021105__wv__general__monongalia__precinct.csv
def remove_invalid_vote_counts(dic: dict):
    value = dic['votes']
    if type(value) == str and value == ' ':
        dic['votes'] = None


def rename_total_to_votes(df: pd.DataFrame):
    if 'total votes' in df.columns:
        return df.rename(columns={'total votes': 'votes'})
    else:
        return df


national_precinct_dataformat = StateDataFormat(
    excluded_files=['20021105__wv__general__monongalia__precinct.csv'],
    df_transformers=[rename_total_to_votes],
    row_cleaners=[remove_invalid_vote_counts])
Exemplo n.º 4
0
from open_elections.tools import StateDataFormat

national_precinct_dataformat = StateDataFormat(df_transformers=[
    lambda df: df.rename(columns={'election_district': 'precinct'})
])
Exemplo n.º 5
0
from open_elections.tools import StateDataFormat, get_coerce_to_integer
import pandas as pd


def fix_vote_counts(df: pd.DataFrame) -> pd.DataFrame:
    if 'total' in df.columns:
        return df.rename(columns={'total': 'votes'})
    else:
        breakout_cols = [
            col for col in ['poll', 'edr', 'abs'] if col in df.columns
        ]
        if breakout_cols:
            temp = df.copy()
            for col in breakout_cols:
                if col in temp.columns:
                    temp[col] = temp[col].apply(
                        get_coerce_to_integer([' -   ']))

            return temp.assign(votes=df[breakout_cols].sum(axis=1))
        else:
            return df


national_precinct_dataformat = StateDataFormat(
    df_transformers=[fix_vote_counts])
Exemplo n.º 6
0
from open_elections.tools import StateDataFormat
import pandas as pd


def rename_total_to_votes(df: pd.DataFrame):
    if 'total_votes' in df.columns:
        return df.rename(columns={'total_votes': 'votes'})
    else:
        return df


national_precinct_dataformat = StateDataFormat(
    df_transformers=[rename_total_to_votes])
Exemplo n.º 7
0
from open_elections.tools import StateDataFormat
import pandas as pd

INVALID_VOTES_VALUES = ['#REF!', 'REP', 'DEM', 'LIB', 'GRN', 'votes']


def remove_invalid_vote_counts(dic: dict):
    value = dic['votes']
    if value in INVALID_VOTES_VALUES:
        dic['votes'] = None
    elif type(value) == str and '*' in value:
        dic['votes'] = int(value.lstrip().replace('*', ''))


def insert_missing_votes_column(df: pd.DataFrame) -> pd.DataFrame:
    if 'early_voting' in df.columns and 'election_day' in df.columns and 'votes' not in df.columns:
        return df.assign(votes=df['early_voting'] + df['election_day'])


national_precinct_dataformat = StateDataFormat(
    df_transformers=[insert_missing_votes_column],
    row_cleaners=[remove_invalid_vote_counts]
)
Exemplo n.º 8
0
    '20160830__az__primary__graham__precinct.csv',
    '20160830__az__primary__santa_cruz__precinct.csv',
    '20160830__az__primary__la_paz__precinct.csv',
    '20160322__az__primary__president__la_paz__precinct.csv',
    '20160322__az__primary__president__apache__precinct.csv',
    '20160830__az__primary__apache__precinct.csv',
    '20160322__az__primary__president__cochise__precinct.csv',
    '20160830__az__primary__mohave__precinct.csv',
    '20160322__az__primary__president__mohave__precinct.csv',
    '20160830__az__primary__yuma__precinct.csv',
    '20160322__az__primary__president__pinal__precinct.csv',
    '20160322__az__primary__president__gila__precinct.csv',
    '20160830__az__primary__gila__precinct.csv',
    '20160322__az__primary__president__yuma__precinct.csv',
    '20160830__az__primary__navajo__precinct.csv',
    '20160830__az__primary__yavapai__precinct.csv',
    '20160322__az__primary__president__coconino__precinct.csv',
    '20160830__az__primary__pima__precinct.csv',
    '20160322__az__primary__president__greenlee__precinct.csv',
    '20160830__az__primary__pinal__precinct.csv',
    '20160322__az__primary__president__maricopa__precinct.csv',
    '20160322__az__primary__president__pima__precinct.csv',
    '20160322__az__primary__president__santa_cruz__precinct.csv',
    '20160830__az__primary__maricopa__precinct.csv',
    '20160830__az__primary__cochise__precinct.csv',
    '20160830__az__primary__coconino__precinct.csv',
    '20160830__az__primary__greenlee__precinct.csv'
]

national_precinct_dataformat = StateDataFormat(excluded_files=INVALID_FILES)
Exemplo n.º 9
0
from open_elections.tools import StateDataFormat


def remove_emtpy_string_vote_counts(dic: dict):
    if dic['votes'] == ' ':
        dic['votes'] = None


national_precinct_dataformat = StateDataFormat(
    row_cleaners=[remove_emtpy_string_vote_counts])
Exemplo n.º 10
0
from open_elections.tools import StateDataFormat

national_precinct_dataformat = StateDataFormat(
    excluded_files=['20081104__hi__general__precinct.csv'])