Skip to content

Commit

Permalink
Removing the setting of results_estimands to nan during historical ru…
Browse files Browse the repository at this point in the history
…ns and the single use of non-conventional column names during estimand creation that I could find so far
  • Loading branch information
dmnapolitano committed Sep 15, 2023
1 parent 2132b43 commit 78ea922
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 18 deletions.
19 changes: 2 additions & 17 deletions src/elexmodel/handlers/data/Estimandizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from numpy import nan


class EstimandException(Exception):
pass

Expand All @@ -18,17 +15,9 @@ def add_estimand_results(self, data_df, estimands, historical):
columns_to_return = []
for estimand in estimands:
results_col = f"{RESULTS_PREFIX}{estimand}"

if results_col not in data_df.columns:
# will raise a KeyError if a function with the same name as `estimand` doesn't exist
data_df = globals()[estimand](data_df, RESULTS_PREFIX)

if historical:
data_df[results_col] = nan
else:
if results_col not in data_df.columns:
raise EstimandException("This is missing results data for estimand: ", estimand)

columns_to_return.append(results_col)

results_column_names = [x for x in data_df.columns if x.startswith(RESULTS_PREFIX)]
Expand Down Expand Up @@ -67,12 +56,8 @@ def add_estimand_baselines(self, data_df, estimand_baselines, historical, includ


def party_vote_share_dem(data_df, col_prefix):
if f"{col_prefix}dem" in data_df.columns and f"{col_prefix}turnout" in data_df.columns:
numer = f"{col_prefix}dem"
denom = f"{col_prefix}turnout"
else:
numer = "dem"
denom = "total"
numer = f"{col_prefix}dem"
denom = f"{col_prefix}turnout"

data_df[f"{col_prefix}party_vote_share_dem"] = data_df.apply(
lambda x: 0 if x[numer] == 0 or x[denom] == 0 else x[numer] / x[denom], axis=1
Expand Down
1 change: 0 additions & 1 deletion tests/handlers/test_estimandizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def test_add_estimand_results_historical(va_governor_county_data):

assert "results_party_vote_share_dem" in output_df.columns
assert result_columns == ["results_party_vote_share_dem"]
assert output_df["results_party_vote_share_dem"].isnull().all()


def test_add_estimand_baselines_not_historical(va_governor_county_data):
Expand Down

0 comments on commit 78ea922

Please sign in to comment.