2014-09-03 16 views
11

Ho la seguente in un dataframe:Conversione fusi orari da panda timestamp

> df['timestamps'].loc[0] 
Timestamp('2014-09-02 20:24:00') 

So che il fuso orario (credo che sia GMT) usa e vorrebbe convertire l'intera colonna a EST. Come posso farlo in Panda?

Per riferimento, ho trovato questi fili:

ma lavorano con datetime timestamp. Es .: fromtimestamp

> datetime.datetime.fromtimestamp(df['timestamps'].loc[0], tz=None) 
returns: 

TypeError         Traceback (most recent call last) 
----> 2 datetime.datetime.fromtimestamp(ts, tz=None) 

TypeError: an integer is required (got type Timestamp) 

risposta

19

Basta usare il metodo tz_convert.

Diciamo che avere un oggetto Timestamp:

stamp = Timestamp('1/1/2014 16:20', tz='America/Sao_Paulo') 
    new_stamp = stamp.tz_convert('US/Eastern') 

Se siete interessati a convertire intervalli di date:

range = date_range('1/1/2014', '1/1/2015', freq='S', tz='America/Sao_Paulo') 
    new_range = range.tz_convert('US/Eastern') 

Per grandi serie storiche:

import numpy as np 
    ts = Series(np.random.randn(len(range)), range) 
    new_ts = ts.tz_convert('US/Eastern') 

Come indicato nel un'altra risposta, se i tuoi dati non hanno un fuso orario impostato, avrai bisogno di tz_localize:

data.tz_localize('utc') 
+0

Grazie, ma quando provo: 'df [ 'timestamp'] [0] .tz_convert ('US/Eastern')' ottengo: 'eccezione: Impossibile convertire timestamp tz-naive, uso tz_localize o localize' –

+2

stesso se provo a lavorare con la colonna direttamente: 'df [ 'timestamp'] tz_convert ('US/Eastern')' ' TypeError:. indice non è un valido o DatetimeIndex PeriodIndex' –

+0

@ user815423426 è necessario per prima cosa tz_localize (come menzionato alla fine della mia risposta), non puoi convertire in un fuso orario se non sai in quale fuso orario hai iniziato. –

3

del datetime è in realtà da un POSIX timestamp ovvero ms dal 1970/01/01 GMT

In [11]: datetime.datetime.fromtimestamp? 
Type:  builtin_function_or_method 
String form: <built-in method fromtimestamp of type object at 0x101d90500> 
Docstring: timestamp[, tz] -> tz's local time from POSIX timestamp. 

In [12]: datetime.datetime.fromtimestamp(0) 
Out[12]: datetime.datetime(1969, 12, 31, 16, 0) 

In [13]: datetime.datetime.fromtimestamp(1) 
Out[13]: datetime.datetime(1969, 12, 31, 16, 0, 1) 

Penso che forse è un problema, come io sono in PST fuso orario.

Questo è diverso da Timestamp panda (anche se sotto il cofano che è ns 1970-1-1).

In [21]: pd.Timestamp(0) 
Out[21]: Timestamp('1970-01-01 00:00:00') 

Per convertire un timestamp/datetime64 uso colonna tz_convert (se la sono tz ingenui, cioè non hanno un fuso orario ancora, è necessario prima tz_localize):

In [31]: pd.Timestamp(0).tz_localize('UTC') 
Out[31]: Timestamp('1970-01-01 00:00:00+0000', tz='UTC') 

In [32]: t = pd.Timestamp(0).tz_localize('UTC') 

In [33]: t.tz_convert('US/Eastern') 
Out[33]: Timestamp('1969-12-31 19:00:00-0500', tz='US/Eastern') 

See il time-zone-handling section of the docs.