A function that turns elasticsearch aggregation buckets into dataframes

        :param name: The name of the bucket (will be a column in the dataframe)
        :type name: str
        :param bucket: a bucket from elasticsearch results
        :type bucket: list[dict]
        :returns: pandas.DataFrame

def bucket_to_dataframe(name, buckets, append_name=None):
    '''A function that turns elasticsearch aggregation buckets into dataframes

        :param name: The name of the bucket (will be a column in the dataframe)
        :type name: str
        :param bucket: a bucket from elasticsearch results
        :type bucket: list[dict]
        :returns: pandas.DataFrame
    '''
    expanded_buckets = []
    for item in buckets:
        if type(item) is dict:
            single_dict = item
        else:
            single_dict = item.to_dict()
        single_dict[name] = single_dict.pop('doc_count')
        if append_name:
            persistance_dict = single_dict.copy()
            for key in persistance_dict.keys():
                single_dict[append_name + '.' + key] = single_dict.pop(key)
        expanded_buckets.append(single_dict)
    return pd.DataFrame(expanded_buckets)