From 19cddaa6347ca6fa743f97003bab7ebdafcb465c Mon Sep 17 00:00:00 2001 From: junos Date: Tue, 27 Jul 2021 18:43:31 +0200 Subject: [PATCH] Convert event time to datetime. --- features/esm_SAM.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/features/esm_SAM.py b/features/esm_SAM.py index f59282c..e4c5adc 100644 --- a/features/esm_SAM.py +++ b/features/esm_SAM.py @@ -39,13 +39,22 @@ def extract_stressful_events(df_esm: pd.DataFrame) -> pd.DataFrame: # 2. 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_event_stress[ GROUP_QUESTIONNAIRES_BY + ["event_present", "event_stressfulness"] ].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 @@ -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, ) 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