我在创建数据框python熊猫时遇到问题

时间:2020-06-16 18:45:59

标签: python pandas elasticsearch

我首先将时间转换为正确的格式,代码工作正常 我在创建数据框时遇到问题,无法解决问题 该代码可以正常工作:

from datetime import datetime as dt


def convert_ts(hit):
    hit = hit['_source']
# change Refill_Bar_End_Date_and_Time
    try:
        ts_from_doc = hit.get('End_Date', None)

        if not ts_from_doc:
            raise ValueError('`End_Date` not found')

        # incoming as millisec so convert to sec
        as_date = dt.fromtimestamp(
            int(ts_from_doc / 1000.0)
        ).strftime('%Y-%m-%d %H:%M:%S')

        hit['End_Date'] = as_date

    except Exception as e:
        print(e)
        pass
    return hit

es = Elasticsearch(['http://localhost:9200'], timeout=600)
documents = es.search(index='sub1', body={})['hits']['hits']
documents = [convert_ts(doc) for doc in documents]

print(documents)

通过这个,我试图从我已经处理过的“文档”中创建数据框

from elasticsearch import Elasticsearch
from pandasticsearch import Select
# Convert the result to Pandas Dataframe
pandas_df = Select.from_dict(documents).to_pandas()

我有错误

----------------------------------------------------------------
AssertionError                 Traceback (most recent call last)
<ipython-input-10-c079591d9fd2> in <module>
      2 from pandasticsearch import Select
      3 # Convert the result to Pandas Dataframe
----> 4 pandas_df = Select.from_dict(documents).to_pandas()
      5 #df = pd.concat(map(pd.DataFrame.from_dict, documents), axis=1)['fields'].T

~/anaconda3/lib/python3.7/site-packages/pandasticsearch/queries.py in from_dict(d)
    113     def from_dict(d):
    114         query = Select()
--> 115         query.explain_result(d)
    116         return query
    117 

~/anaconda3/lib/python3.7/site-packages/pandasticsearch/queries.py in explain_result(self, result)
     88 
     89     def explain_result(self, result=None):
---> 90         super(Select, self).explain_result(result)
     91         rows = []
     92         for hit in self._result_dict['hits']['hits']:

~/anaconda3/lib/python3.7/site-packages/pandasticsearch/queries.py in explain_result(self, result)
     17     def explain_result(self, result=None):
     18         if result is not None:
---> 19             assert isinstance(result, dict)
     20             self._result_dict = result
     21             self._took_millis = self._result_dict['took']

AssertionError: 

0 个答案:

没有答案