1

Working on Python, I need to convert an array of datetime values into sample times, because I want to treat the corresponding time of the time series as sampletime [0..T].

[2013/11/09 14:29:54.660, 2013/11/09 14:29:54.680, ... T] where T> 1000. So I have an array of >1000 date time values, pretty big

I come up with the following code:

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]

This piece of code seem to work well, but I have batches of 1000 samples within the signal:

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

So, my resulting signal is not a continuos one. How do I properly do this conversion in order to keep a continuous signal? Anybody has a good idea on how to solve this?

fran_jo
  • 69
  • 9
  • Your code looks fine to me. What is the problem? You would need to share some more data! – Anton vBR Aug 07 '17 at 13:16
  • @AntonvBR look at the last array, with the resulting sample times. It starts at 0 over and over again. So, when you plot the signal with the new array of sample times, it is a mess. I am checking the pandas library, but I don't get a clear idea on how to do it – fran_jo Aug 07 '17 at 13:22
  • oh ofc, it because microseconds reset – Anton vBR Aug 07 '17 at 13:24
  • @fran_jo: what time origin do you want? Apparently you tried with the origine of a time series and you did not like it. Why don't you just take an arbitrary date (datetime) that you would convert the same? – Serge Ballesta Aug 07 '17 at 13:35
  • @SergeBallesta I don't think I completely understand... the origin of my data is the first value of the array, this has to be the sample time 0. But the problem I faced was the "cycles" on the resulting array (batches of 1000 samples from 0..980 in an array of X>1000 samples) – fran_jo Aug 07 '17 at 16:02

1 Answers1

3

Use total_seconds() which also works for timedeltas: Convert TimeDiff to total seconds

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

Working example:

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]
Anton vBR
  • 18,287
  • 5
  • 40
  • 46