Convert event time to datetime.

communication
junos 2021-07-27 18:43:31 +02:00
parent 763b970a42
commit 19cddaa634
1 changed files with 21 additions and 1 deletions

View File

@ -39,13 +39,22 @@ def extract_stressful_events(df_esm: pd.DataFrame) -> pd.DataFrame:
# 2. # 2.
df_esm_event_stress = detect_stressful_event(df_esm_sam_clean) df_esm_event_stress = detect_stressful_event(df_esm_sam_clean)
# Join all the features related to the events. # Join to the previously calculated features related to the events.
df_esm_events = df_esm_event_threat_challenge_mean_wide.join( df_esm_events = df_esm_event_threat_challenge_mean_wide.join(
df_esm_event_stress[ df_esm_event_stress[
GROUP_QUESTIONNAIRES_BY + ["event_present", "event_stressfulness"] GROUP_QUESTIONNAIRES_BY + ["event_present", "event_stressfulness"]
].set_index(GROUP_QUESTIONNAIRES_BY) ].set_index(GROUP_QUESTIONNAIRES_BY)
) )
# 3.
df_esm_event_time = convert_event_time(df_esm_sam_clean)
df_esm_events = df_esm_events.join(
df_esm_event_time[GROUP_QUESTIONNAIRES_BY.append("event_time")].set_index(
GROUP_QUESTIONNAIRES_BY
)
)
return df_esm_events return df_esm_events
@ -98,3 +107,14 @@ def detect_stressful_event(df_esm_sam_clean: pd.DataFrame) -> pd.DataFrame:
event_stressfulness=lambda x: x.esm_user_answer_numeric, event_stressfulness=lambda x: x.esm_user_answer_numeric,
) )
return df_esm_event_stress return df_esm_event_stress
def convert_event_time(df_esm_sam_clean: pd.DataFrame) -> pd.DataFrame:
df_esm_event_time = df_esm_sam_clean[
df_esm_sam_clean["questionnaire_id"] == QUESTIONNAIRE_ID_SAM.get("event_time")
].assign(
event_time=lambda x: pd.to_datetime(
x.esm_user_answer, errors="coerce", infer_datetime_format=True, exact=True
)
)
return df_esm_event_time