Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added filter to remove rows with null values for partial turnout and registered voters #939

Merged
merged 1 commit into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apollo/result_analysis/turnout.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def turnout_convergence(form_id, location_id=None):
summary = {}

for turnout_field_label in turnout_field_labels:
valid_dataset = valid_turnout_dataframe(dataset, turnout_field_label, 'registered_voters') # noqa
valid_dataset = valid_turnout_dataframe(dataset, form, turnout_field_label, 'registered_voters') # noqa

turnouts_count = valid_dataset.shape[0]
turnouts_reported = valid_dataset.notna().sum()
Expand Down
18 changes: 16 additions & 2 deletions apollo/submissions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,19 @@ def make_turnout_dataframe(query, form): # noqa

return df_summary

def valid_turnout_dataframe(dataframe: pd.DataFrame, turnout_field: str, rv_field: str): # noqa
return dataframe[(dataframe[rv_field] > 0) & dataframe[turnout_field].notna()] # noqa
def valid_turnout_dataframe(dataframe: pd.DataFrame, form: object, turnout_field_label: str, rv_field_label: str): # noqa
turnout_field = form.get_field_by_tag(turnout_field_label)
turnout_rv_field = form.get_field_by_tag(form.turnout_registered_voters_tag) # noqa
if turnout_rv_field:
return dataframe[
(dataframe[rv_field_label] > 0) &
(dataframe[rv_field_label] != turnout_rv_field.get('null_value', pd.np.nan)) & # noqa
dataframe[turnout_field['tag']].notna() &
(dataframe[turnout_field['tag']] != turnout_field.get('null_value', pd.np.nan)) # noqa
]
else:
return dataframe[
(dataframe[rv_field_label] > 0) &
dataframe[turnout_field['tag']].notna() &
(dataframe[turnout_field['tag']] != turnout_field.get('null_value', pd.np.nan)) # noqa
]