Reputation: 141
I have data in two numpy
arrays:
data = [0.1, 0.3, 0.4, 0.6, 0.7]
time = [10,25, 27, 35, 42]
data
corresponds to a value at the equivalent index of time
I want to create another array based a new time
array that looks like this:
newtime = [10, 20, 30, 40, 50]
The new data array should look like this:
[0.1, 0.1, 0.4,0.6, 0.7]
Which corresponds to the value of data
at each of the new times. For example, at 10s
, the value was 0.1
, and at 20
seconds the value had not changed, so both should be 0.1
The question is what's the easiest possible way to create the "new data" numpy from the data array and time array in python? I would be happy to try other python libraries as well other than numpy.
Upvotes: 1
Views: 1564
Reputation: 94
For this tasks you can use RedBlackPy, it is supported for Python 3 on linux and macosx. RedBalckPy.Series class supports convenient functionality for time series, it keeps keys always sorted. You don't need to create new Series in your case, because interpolation is built into getitem operator(Series[key]). You have access at any key using interpolation(in your case floor). See the following code example:
import redblackpy as rb
# yours data and time
data = [0.1, 0.3, 0.4, 0.6, 0.7]
time = [10,25, 27, 35, 42]
# create Series object with floor interpolation type
series = rb.Series( index=time, values=data, dtype='float64',
interpolate='floor' )
# now you can access at any key using interpolation,
# it does not create new element, it just use neighbours (t_1 < key < t_2)
# in our case we set floor interpolation, so we get value for t_1
print(series[10]) # prints 0.1
print(series[20]) # prints 0.1
print(series[30]) # prints 0.4
print(series[40]) # prints 0.6
print(series[50]) # prints 0.7
# print Series, Series does not insert this keys
print(Series)
Last print gives the following:
Series object Untitled
10: 0.1
25: 0.3
27: 0.4
35: 0.6
42: 0.7
For more code examples, see article on TowardsDataScience.
Upvotes: 0
Reputation: 1269
Using searchsorted probably is one of the fastest ways
import numpy as np
data = np.array([0.1, 0.3, 0.4, 0.6, 0.7])
time = np.array([10,25, 27, 35, 42])
newtime =np.array([10, 20, 30, 40, 50])
newdata = data[ np.searchsorted(time, newtime, side="right") - 1 ]
print(newdata)
This prints out
[ 0.1 0.1 0.4 0.6 0.7]
Upvotes: 1