我正在使用 python-elasticsearch api 上的 fields 引數從 elasticsearch 中檢索一些資料,嘗試決議 iso 格式的 @timestamp,以用于 Pandas 資料幀。
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.
因此,生成的資料幀包含一個物件串列系列,無法通過常規方法決議為日期時間系列。
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]
嘗試將系列決議為日期時間系列時:
pd.to_datetime(["fields.@timestamp"])
結果是:
TypeError: <class 'list'> is not convertible to datetime
我的用例需要很多日期時間格式和欄位引數非常適合查詢多種格式,但是列出的物件日期時間字串很難處理。
uj5u.com熱心網友回復:
這里的問題是 fields.@timestamp 的專案實際上是串列。
所以你可以這樣做:
fields['timestamp'] = fields['timestamp'].str[0]
從串列中提取日期,然后使用 pd.to_datetime :
fields['timestamp'] = pd.to_datetime(fields['timestamp'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/360961.html
上一篇:合并資料幀的非重復行
