在python中创建时间序列数据
我有两个在python中创建时间序列数据,python,arrays,numpy,time-series,Python,Arrays,Numpy,Time Series,我有两个numpy数组中的数据: data = [0.1, 0.3, 0.4, 0.6, 0.7] time = [10,25, 27, 35, 42] 数据对应于时间 我想创建另一个基于新time数组的数组,如下所示: newtime = [10, 20, 30, 40, 50] [0.1, 0.1, 0.4,0.6, 0.7] 新的数据阵列应如下所示: newtime = [10, 20, 30, 40, 50] [0.1, 0.1, 0.4,0.6, 0.7] 它对应于每个新时间
numpy
数组中的数据:
data = [0.1, 0.3, 0.4, 0.6, 0.7]
time = [10,25, 27, 35, 42]
数据
对应于时间
我想创建另一个基于新time
数组的数组,如下所示:
newtime = [10, 20, 30, 40, 50]
[0.1, 0.1, 0.4,0.6, 0.7]
新的数据阵列应如下所示:
newtime = [10, 20, 30, 40, 50]
[0.1, 0.1, 0.4,0.6, 0.7]
它对应于每个新时间的数据值。例如,在10s
时,该值为0.1
,在20
秒时,该值未发生变化,因此两者都应为0.1
问题是用python中的数据数组和时间数组创建“新数据”numpy的最简单方法是什么?除了numpy,我也很乐意尝试其他python库。使用searchsorted可能是最快的方法之一
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)
这是打印出来的
[ 0.1 0.1 0.4 0.6 0.7]
对于您可以使用的这些任务,linux和macosx上的Python3支持它。Series类支持方便的时间序列功能,它使键始终保持排序。在本例中,您不需要创建新的序列,因为插值内置于getitem操作符(Series[key])。您可以使用插值(在您的情况下是楼层)访问任意关键点。请参见以下代码示例:
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)
有关更多代码示例,请参阅上的文章。也许您可以使用dict