Pandas objet-รายการ datetime serie จะ datetime ดัชนี

0

คำถาม

ฉันใช้พารามิเตอร์ฟิลด์ที่เป็นภาษาไพธอน-elasticsearch รูปแบบ api ที่เก็บข้อมูลบางอย่างจาก elasticsearch พยายามที่จะวิเคราะห์@วันที่และเวลาในรูปแบบมาตรฐาน iso สำหรับใช้ใน pandas dataframe.

fields = \
    [{
      "field": "@timestamp",
      "format": "strict_date_optional_time"
    }]

ค่าปริยายของ elasticsearch อบแทนที่ผลตรวจในอาเรย์-รายการรูปแบบที่เห็นในหมอ:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-fields.html

The fields response always returns an array of values for each field, even when there is a single value in the _source. 

เนื่องจากมันผลลัพธ์จาก dataframe มีวัตถุ-รายการ serie ที่ไม่สามารถต่างๆที่วิเคราะห์ต้องเป็น datetime serie โดยบูวิธีการของ.

Name: fields.@timestamp, Length: 18707, dtype: object
0       [2021-11-04T01:30:00.263Z]
1       [2021-11-04T01:30:00.385Z]
2       [2021-11-04T01:30:00.406Z]
3       [2021-11-04T01:30:00.996Z]
4       [2021-11-04T01:30:01.001Z]
                   ...            
8368    [2021-11-04T02:00:00.846Z]
8369    [2021-11-04T02:00:00.894Z]
8370    [2021-11-04T02:00:00.895Z]
8371    [2021-11-04T02:00:00.984Z]
8372    [2021-11-04T02:00:00.988Z]

ตอนที่พยายามที่จะวิเคราะห์ serie จะ datetime serie:

pd.to_datetime(["fields.@timestamp"])

นั่นผลอยู่ใน:

TypeError: <class 'list'> is not convertible to datetime

ฉันใช้กรณีต้องการมากมาย datetime รูปแบบและช่องข้อมูลพารามิเตอร์เหมาะกับดี querying หลายในรูปแบบ,แต่บมีวัตถุ datetime ข้อความลำบากอย่าง.

dataframe datetime elasticsearch pandas
2021-11-18 16:37:23
1

คำตอบที่ดีที่สุด

1

เรื่องที่สำคัญที่นี่คือรายการของ fields.@timestamp คือที่จริงรายการ

ดังนั้นคุณจะทำอย่าง:

fields['timestamp'] = fields['timestamp'].str[0]

ที่สามารถถอดเอาวันอกจากรายการ แล้วใช้ทางตำรวจ.to_datetime:

fields['timestamp'] = pd.to_datetime(fields['timestamp'])
2021-11-18 17:01:03

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่