I have a time series that looks like:
timeseries1 = [{'price': 250, 'time': 1.52},
{'price': 251, 'time': 3.65},
{'price': 253, 'time': 10.1},
{'price': 254, 'time': 10.99}]
I want to be able to interpolate this data so that it moves forward in small timesteps, and have something like:
timeStep = 0.1
timeseries2 = [{'price': 250, 'time': 1.5},
{'price': 250, 'time': 1.6},
{'price': 250, 'time': 1.7},
...
{'price': 250, 'time': 3.6},
{'price': 251, 'time': 3.7},
{'price': 251, 'time': 3.8},
{'price': 251, 'time': 3.9},
...
{'price': 251, 'time': 10.0},
{'price': 253, 'time': 10.1},
{'price': 253, 'time': 10.2},
{'price': 253, 'time': 10.3},
...
{'price': 253, 'time': 10.9},
{'price': 254, 'time': 11.0}]
I'm really unsure of how to do this efficiently and hope there will be a nice pythonic way to do so. What I've tried doing is iterating through timeseries1, with a while loop to append new values to the end of timeseries2, but this seems very inefficient having 2 nested loops.
Edit: Here is the code/algorithm currently being used to do this.
startTime = math.floor(timeseries1[0]['time'] / timeStep) * timeStep
oldPrice = timeseries1[0]['price']
timeseries3 = []
timeseries3.append(timeseries1[0])
timeseries3[0]['time'] = startTime
for x in timeseries1[1:]:
while startTime < x['time']:
timeseries3.append({'price': oldPrice, 'time': startTime})
startTime += timeStep
oldPrice = x['price']
So that timeseries3 will be the same as timeseries2 in the end.