2015-12-02 261 views
6

我想將Datetime(2014-12-23 00:00:00)更改爲unixtime。我用Datetime函數試過了,但它沒有工作。我在數組中獲得了Datetime郵票。熊貓日期時間到unixtime

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

非常感謝

+0

假設你的D型是'datetime64'那麼下面(t.astype(np.int64)/ 1e6).astype(np.uint64)' – EdChum

+0

另一種方法是:'(t-dt.datetime(1970,1,1))。dt .total_seconds()' – EdChum

+0

非常感謝。你也有我的正確答案:) –

回答

8

我想你可以減去日期1970-1-1創建timedelta然後訪問屬性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

我如何將1h添加到unixtime ,因爲我住在UTC:+ 1h –

+0

你可以在上面的計算之前添加一個timedelta:'s + pd.Timedelta(1,'h')'或者你的情況't + pd.Timedelta(1,'h ')' – EdChum

+1

其實正確的方法是讓日期時間知道,例如's.dt.tz_localize('utc')'或任何你想要的時區 – EdChum