2017-08-07 61 views
1

使用Python時,我需要將一組日期時間值轉換爲採樣時間,因爲我想將時間序列的相應時間視爲採樣時間[0..T]。將數據時間格式轉換爲採樣時間

[2013/11/09 14:29:54.660, 2013/11/09 14:29:54.680, ... T]其中T> 1000所以我> 1000個日期時間值的數組,相當大的

我想出了下面的代碼:

tiempos= [datetime.strptime(x,"%Y/%m/%d %H:%M:%S.%f") for x in csvTimeColum] 
sampletime= [(t- tiempos[0]).microseconds/1000 for t in tiempos] 

這段代碼似乎工作好吧,但我有批量的信號內的1000個樣本:

[0,20,...,980,0,20,...,980,0,20,...,980,...] 

所以,我的結果信號是不連續的。如何正確進行這種轉換以保持連續的信號?任何人對如何解決這個問題都有一個好主意?

+0

您的代碼看起來好像沒什麼問題。問題是什麼?你需要分享更多的數據! –

+0

@AntonvBR查看最後一個數組,結果採樣時間。它從0開始一遍又一遍。所以,當你用新的採樣時間繪製信號時,這是一團糟。我正在檢查熊貓圖書館,但我不明白如何去做 –

+0

哦,因爲微秒重置 –

回答

3

使用total_seconds(),它也適用於timedeltas: Convert TimeDiff to total seconds

sampletime= [(t- tiempos[0]).total_seconds()*1000 for t in tiempos] 

工作例如:

import datetime 
csvTimeColum = ["2013/11/09 14:29:54.660", "2013/11/09 14:29:54.680"] 
tiempos= [datetime.datetime.strptime(x,"%Y/%m/%d %H:%M:%S.%f") for x in csvTimeColum] 
sampletime= [(t- tiempos[0]).total_seconds()*1000 for t in tiempos] 
sampletime # [0.0, 20.0] 
+0

謝謝@AntonvBR。這就是我一直在尋找的! –