Takes in aggregation buckets and converts them into a pandas dataframe
    after cleaning the buckets. If a DateTime field is present(usually having the name:
    "key_as_string") parses it to datetime object and then it uses it as key

    :param buckets: elasticsearch aggregation buckets to be converted to a DataFrame obj
    :returns: a DataFrame object created by parsing the buckets

def buckets_to_df(buckets):
    """
    Takes in aggregation buckets and converts them into a pandas dataframe
    after cleaning the buckets. If a DateTime field is present(usually having the name:
    "key_as_string") parses it to datetime object and then it uses it as key

    :param buckets: elasticsearch aggregation buckets to be converted to a DataFrame obj
    :returns: a DataFrame object created by parsing the buckets
    """
    cleaned_buckets = []
    for item in buckets:
        if type(item) == str:
            return item

        temp = {}
        for key, val in item.items():
            try:
                temp[key] = val['value']
            except Exception as e:
                temp[key] = val

        cleaned_buckets.append(temp)

    if "key_as_string" in temp.keys():
        ret_df = pd.DataFrame.from_records(cleaned_buckets)
        ret_df = ret_df.rename(columns={"key": "date_in_seconds"})
        ret_df['key'] = pd.to_datetime(ret_df['key_as_string'])
        ret_df = ret_df.drop(["key_as_string", "doc_count"], axis=1)
        ret_df = ret_df.set_index("key")
    elif "key" in cleaned_buckets[0].keys():
        ret_df = pd.DataFrame.from_records(cleaned_buckets)  # index="key")
    else:
        ret_df = pd.DataFrame(cleaned_buckets)

    return ret_df.fillna(0)