def expand_results()

in apps/cloudwatch-dashboard/layers/lookoutequipment/python/l4ecwcw.py [0:0]


def expand_results(df):
    """
    Let's first expand the results to expose the content of the diagnostics 
    column above into different dataframe columns
    """
    expanded_results = []
    for _, row in df.iterrows():
        new_row = dict()
        new_row.update({'start': row['start']})
        new_row.update({'end': row['end']})
        new_row.update({'prediction': 1.0})

        diagnostics = pd.DataFrame(row['diagnostics'])
        diagnostics = dict(zip(diagnostics['name'], diagnostics['value']))
        new_row = {**new_row, **diagnostics}

        expanded_results.append(new_row)

    expanded_results = pd.DataFrame(expanded_results)
    tags_list = expanded_results.columns[3:]
    tags_list = {t.split('\\')[-1]: t for t in tags_list}
    
    df_list = []
    for _, row in expanded_results.iterrows():
        new_index = pd.date_range(start=row['start'], end=row['end'], freq='1T')
        new_df = pd.DataFrame(index=new_index)

        for tag, col in tags_list.items():
            new_df[tag] = row[col]

        df_list.append(new_df)

    expanded_results_v2 = pd.concat(df_list, axis='index')
    
    return expanded_results_v2