def date_dict(df_list, pif_key): encounter_dates = [] for df in df_list: df['encounter_date'].fillna('', inplace=True) df_date = df.loc[df['pif_key'].astype(str) == str(pif_key), 'encounter_date'].values if len(df_date) > 0: encounter_dates.extend(df_date) if encounter_dates: ingested_date = max(encounter_dates) else: ingested_date = '' date_insert_dict = { 'attribute_name': 'report_date', 'attribute_method': 'cv', 'attribute_normalized_prediction': '', 'attribute_prediction': str(ingested_date), 'attribute_version': 'v2_090523', 'attribute_vocab': '', 'attribute_code': '', 'date_of_service': '' } return date_insert_dict, encounter_dates, ingested_date def report_date_insertion(dict_list, df_list, logging_df): col_names = {col['attribute_name'] for col in dict_list} if 'report_date' not in col_names: pif_key = next((col['attribute_prediction'] for col in dict_list if col['attribute_name'] == 'pif_key'), None) if pif_key is not None: date_insert_dict, encounter_dates, ingested_date = date_dict(df_list, pif_key) dict_list.insert(1, date_insert_dict) if ingested_date: if len(encounter_dates)>1: logging_df = logging_df.append({'pif_key': pif_key, 'json_report_date_exists': False, 'encounter_dates': encounter_dates, 'ingested_date': ingested_date, 'multiple_date': True, 'old_date': 'missing'}, ignore_index=True) else: logging_df = logging_df.append({'pif_key': pif_key, 'json_report_date_exists': False, 'encounter_dates': encounter_dates, 'ingested_date': ingested_date, 'multiple_date': False, 'old_date': 'missing'}, ignore_index=True) else: logging_df = logging_df.append({'pif_key': pif_key, 'json_report_date_exists': False, 'encounter_dates': None, 'ingested_date': '', 'multiple_date': False, 'old_date': 'missing'}, ignore_index=True) elif 'report_date' in col_names: pif_key = next((col['attribute_prediction'] for col in dict_list if col['attribute_name'] == 'pif_key'), None) if pif_key is not None: date_insert_dict, encounter_dates, ingested_date = date_dict(df_list, pif_key) if ingested_date: for report_date_idx, tm in enumerate(dict_list): if tm['attribute_name']=='report_date': old_date = tm['attribute_prediction'] break dict_list.pop(report_date_idx) dict_list.insert(1, date_insert_dict) if len(encounter_dates)>1: logging_df = logging_df.append({'pif_key': pif_key, 'json_report_date_exists': True, 'encounter_dates': encounter_dates, 'ingested_date': ingested_date, 'multiple_date': True, 'old_date': old_date}, ignore_index=True) else: logging_df = logging_df.append({'pif_key': pif_key, 'json_report_date_exists': True, 'encounter_dates': encounter_dates, 'ingested_date': ingested_date, 'multiple_date': False, 'old_date': old_date}, ignore_index=True) else: for report_date_idx, tm in enumerate(dict_list): if tm['attribute_name']=='report_date': old_date = tm['attribute_prediction'] break logging_df = logging_df.append({'pif_key': pif_key, 'json_report_date_exists': True, 'encounter_dates': None, 'ingested_date': '', 'multiple_date': False, 'old_date': old_date}, ignore_index=True) return dict_list, logging_df def json_report_date_insertion(json_data, df_list, logging_df): for biomarker_detail in json_data['patient_level']['biomarkers']['details']: for attribute in biomarker_detail['attribute']: attribute_details = attribute['attribute_details'] dict_list, logging_df = report_date_insertion(attribute_details, df_list, logging_df) attribute['attribute_details'] = dict_list return json_data, logging_df