2015-12-02 19 views
6

Desidero modificare Datetime (2014-12-23 00:00:00) in unixtime. L'ho provato con la funzione Datetime ma non ha funzionato. Ho ottenuto i timbri di Data/ora in un array.Pandas datetime to unixtime

Zeit =np.array(Jahresgang1.ix[ :,'Zeitstempel']) 
t = pd.to_datetime(Zeit, unit='s') 
unixtime = pd.DataFrame(t) 
print unixtime 

Grazie mille

+0

Supponendo che il vostro DTYPE è 'datetime64' allora la seguente sembra fare quello che vuoi: '(t.astype (np.int64)/1e6) .astype (np.uint64)' – EdChum

+0

Un altro metodo è: '(t-dt.datetime (1970,1,1)). dt .total_seconds() ' – EdChum

+0

Grazie mille ancora e ancora. Hai sempre le risposte giuste per me :) –

risposta

8

Penso che si possa sottrarre la data di 1970-1-1 per creare un timedelta e quindi accedere l'attributo total_seconds:

In [130]:  
s = pd.Series(pd.datetime(2012,1,1)) 
s 

Out[130]: 
0 2012-01-01 
dtype: datetime64[ns] 

In [158]: 
(s - dt.datetime(1970,1,1)).dt.total_seconds() 

Out[158]: 
0 1325376000 
dtype: float64 
+0

Come posso aggiungere 1h all'unxtime , perché vivo in UTC: + 1h –

+0

Puoi aggiungere una timedelta prima dei calcoli sopra: 's + pd.Timedelta (1, 'h')' o nel tuo caso 't + pd.Timedelta (1, 'h ') ' – EdChum

+1

In realtà il modo corretto sarebbe quello di rendere noto il timezone dei tempi, ad es. 's.dt.tz_localize ('utc')' o qualsiasi fuso orario che vuoi – EdChum