Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中创建时间序列数据_Python_Arrays_Numpy_Time Series - Fatal编程技术网

在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