有没有办法在Python中找到两个时间戳数组的匹配元素?
我正在尝试为两个数组执行此操作:
import numpy as np
import pandas
xrp[:,0]
Out[135]:
array([Timestamp('2018-03-08 00:00:00'), Timestamp('2018-03-07 00:00:00'),
Timestamp('2018-03-06 00:00:00'), ...,
Timestamp('2013-08-06 00:00:00'), Timestamp('2013-08-05 00:00:00'),
Timestamp('2013-08-04 00:00:00')], dtype=object)
btc[:,0]
Out[136]:
array([Timestamp('2018-03-08 00:00:00'), Timestamp('2018-03-07 00:00:00'),
Timestamp('2018-03-06 00:00:00'), ...,
Timestamp('2013-04-30 00:00:00'), Timestamp('2013-04-29 00:00:00'),
Timestamp('2013-04-28 00:00:00')], dtype=object)
val = np.where(btc[:,0]==xrp[:,0])
__main__:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
val
Out[138]: (array([], dtype=int64),)
似乎np.where和Timestamps不是很兼容,或者过去常常不再存在。
如果可能,我想在没有循环的情况下这样做。
每个数组的形状为:
xrp[:,0].shape
Out[139]: (1678,)
btc[:,0].shape
Out[140]: (1776,)
答案 0 :(得分:1)
Let a
and b
be your arrays
i, j = np.where(a == b[:, np.newaxis])
print(a[i])
print(b[j])