Source code for pollscraper.trends

import pandas as pd
import numpy as np
from pollscraper import logger
from datetime import datetime
from pandas.api.types import is_datetime64_any_dtype as is_datetime


[docs]class PollTrend: """ Represents poll trends and provides methods to calculate trends. This class calculates average poll trends based on poll data. Attributes: None """
[docs]def check_for_outliers_in_poll_averages( poll_averages, avg, sig, n_sigma, candidate ): avg_outliers = poll_averages.loc[ np.abs(poll_averages - avg) >= n_sigma * sig ] if not avg_outliers.empty: logger.warning(f'Checking averaged polls for candidate {candidate}.') logger.warning(f'Found {avg_outliers.shape[0]} poll averages detected ' f'at > {n_sigma} sigma from the mean') return avg_outliers
[docs]def check_for_outliers_in_individual_polls( poll_data, candidate, avg, sig, n_sigma ): rolling = pd.DataFrame() rolling['sigma_band'] = sig rolling['rolling_avg'] = avg # Left join of the rolling variables to the individual polls check_individual_polls = poll_data[candidate].to_frame()\ .join(rolling) individual_outliers = check_individual_polls.loc[ np.abs( check_individual_polls[candidate] - check_individual_polls['rolling_avg'] ) >= n_sigma * check_individual_polls['sigma_band'] ] if not individual_outliers.empty: logger.warning(f'Checking individual polls for candidate {candidate}.') logger.warning(f'Found {individual_outliers.shape[0]} individual ' f'polls detected at > {n_sigma} sigma from the mean') return individual_outliers[candidate]