Panda objet-elenco datetime serie datetime indice

0

Domanda

Sto usando il parametro campi su python-elasticsearch api per recuperare alcuni dati elasticsearch cercando di analizzare il @timestamp in formato iso, per l'uso in una panda dataframe.

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

Per impostazione predefinita, elasticsearch restituire i risultati su array-elenco formato, come si è visto nel doc:

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. 

A causa di questo la conseguente dataframe contiene un oggetto lista di serie che non può essere analizzato per un valore di tipo datetime serie con i metodi convenzionali.

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]

Quando si cerca di analizzare il campionato di serie a datetime serie:

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

Che si traduce in:

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

Il mio caso d'uso richiede un sacco di formati di data / ora e campi di parametro che si addice molto bene query su più formati, ma il quotati oggetto datetime stringa difficile le cose.

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

Migliore risposta

1

Il problema qui è che gli elementi di fields.@timestamp sono in realtà liste.

Così si potrebbe fare :

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

per estrarre la data nell'elenco, e quindi utilizzare pd.to_datetime :

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

In altre lingue

Questa pagina è in altre lingue

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