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

623 refactor create missing questions #121

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
38 changes: 25 additions & 13 deletions mbs_results/staging/create_missing_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ def create_missing_questions(
mapper: dict,
) -> pd.DataFrame:
"""
Adds missing questions to responses data by comparing dataframes.
Reference, period, formid and question_no uniquely identify rows in responses data.
Reference, period and formid to uniquely identify rows in contributors data.
Uses mapper to create all expected questions which should be present in the
responses dataframe.
Adds missing questions to responses data by checking which question numbers
are expected in contributors_df.

A mapper dictionary needs to be passed which maps form id with expected
question numbers, the function will then check which question numbers
(per reference,period) exist in responses_df and will add empty rows
if it does not exist.

Note:

Reference, period and question_no uniquely identify rows in responses data.
Reference, period and formid to uniquely identify rows in contributors data.

Parameters
----------
Expand All @@ -41,9 +47,9 @@ def create_missing_questions(

"""

actual_responses_df = responses_df.filter([reference, period, formid, question_no])
question_no_in_responses = responses_df.filter([reference, period, question_no])

expected_responses = (
expected_question_no = (
contributors_df.filter([reference, period, formid]) # Select needed fields
.assign(
**{question_no: contributors_df[formid].map(mapper)}
Expand All @@ -52,13 +58,19 @@ def create_missing_questions(
.explode(question_no) # Convert questions to rows
)

anti_join_df = expected_responses.merge(
actual_responses_df, how="left", indicator=True
)
anti_join_df = anti_join_df[anti_join_df["_merge"] == "left_only"].drop(
columns="_merge"
joined_question_no = expected_question_no.merge(
question_no_in_responses,
how="left",
on=[reference, period, question_no],
indicator=True,
)

concatenated_responses = pd.concat([responses_df, anti_join_df], ignore_index=True)
missing_responses = joined_question_no[
joined_question_no["_merge"] == "left_only"
].drop(columns="_merge")

concatenated_responses = pd.concat(
[responses_df, missing_responses], ignore_index=True
)

return concatenated_responses
Loading