I have a recording with the start time (t0) and length of the recording (N). How do I create a time series vector (ts) in python for every 10s increments?
For example:
t0 = 2017-06-12T11:05:10.00
N=1000
So there should be an array of 100 (N/10)values such that:
ts = [2017-06-12T11:05:10.00, 2017-06-12T11:05:20.00,2017-06-12T11:05:30.00 and so on...]
CodePudding user response:
import datetime
import numpy as np
t0=datetime.datetime(2017, 6, 12, 11, 5, 10)
dt=datetime.timedelta(seconds=10)
ts=np.arange(100)*ts t0
There might be some easier way. I've never tried to find one. But this is how I do it.
CodePudding user response:
You can use the datetime
module of Python.
First you need to convert your string into a date with dateteime.strptime
:
t0 = datetime.datetime.strptime("2017-06-12T11:05:10.00", "%Y-%m-%dT%H:%M:%S.00")
where the "%Y-%m-%dT%H:%M:%S.00"
part is the description of your string format (see documentation)
Then you can increment a datetime
object by adding a timedelta
to it. Build a sequence like this:
delta = datetime.timedelta(seconds=10)
ts = [t0 i*delta for i in range(N)]
You can also recover dates as strings by using datetime.strftime
with a similar syntax to strptime
.
The whole thing would look like
from datetime import datetime, timedelta
date_format = "%Y-%m-%dT%H:%M:%S.00"
t0 = datetime.strptime("2017-06-12T11:05:10.00", date_format)
delta = timedelta(seconds=10)
ts = [datetime.strftime(t0 i * delta, date_format) for i in range(100)]