简体   繁体   English

在numpy数组中返回相邻值的最快方法

[英]Fastest way to return adjacent value in numpy array

I have two numpy arrays that hold dates and times.我有两个保存日期和时间的 numpy 数组。 The times on each day don't necessarily match so I need to run through to check to find a match that does.每天的时间不一定匹配,所以我需要运行检查以找到匹配的匹配项。

As such, I have currently created a simple UDF that returns the time of the date passed through.因此,我目前创建了一个简单的 UDF,它返回通过的日期时间。 But this take a long time since the arrays are quite large.但这需要很长时间,因为数组非常大。

Below is my current code, I have broken it down to it's basic principles:以下是我当前的代码,我已将其分解为基本原则:

import numpy as np

#my arrays
arr1 = np.array([[20/12/2019, 16:00], [21/12/2019, 12:00], [22/12/2019, 15:00]])
arr2 = np.array([[20/12/2019, 16:00], [21/12/2019, 15:00], [22/12/2019, 16:00]])

#udf
def get_time(index_date):
    for i in range(arr2.shape[0]-1):
        if arr2[i,0] == index_date:
            return arr2[i,1]
            break

#loop through main data
for i in range(arr1.shape[0]-1):
    if arr1[i,1] = get_time(arr1[i,0]):
        print('match')
        break

The above works, but is quite slow and cumbersome.上述工作,但相当缓慢和麻烦。 I know there is also something like the below:我知道还有类似下面的内容:

if value in my_array[:, col_num]:

But this would not return the adjacent time, it would only check to see if a date exists.但这不会返回相邻的时间,它只会检查日期是否存在。

My question:我的问题:

What is the fastest way to loop through a numpy array to return an adjacent value once an index is found?一旦找到索引,循环遍历 numpy 数组以返回相邻值的最快方法是什么?

This is a workaround and only useful if you can't use Rafael's solution.这是一种解决方法,仅您无法使用 Rafael 的解决方案时才有用。 We replace the date in a temporary array to check which coordinates have a custom value.我们替换临时数组中的日期以检查哪些坐标具有自定义值。

You can use您可以使用

for row in np.array(np.where(np.core.defchararray.replace(p,old="21/12/2019",new="")==''))[0]:
    #Prints time where date == 21/12/2019
    print(p[row,1])

This will find the row coordinate of the target date, and that can get you the next column which in your case is the time.这将找到目标日期的行坐标,并且可以为您提供下一列,在您的情况下是时间。 It's trivial to check multiple values, iterating over the values which you want to check and replacing the inner old values.检查多个值、迭代要检查的值并替换内部旧值很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM