如何让numpy函数只对来自两个不同数组的同一组索引执行操作?

eoxn13cs  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(121)

我有四个数组:
per_1_start_time = [[Timestamp('2014 -01-20 13:06:00+ 0100',tz='pytz.FixedOffset(60)')] [Timestamp('2014 -02-04 10:31:00+ 0100',tz='pytz.FixedOffset(60)')]]
per_1_stop_time = [[Timestamp('2014 -01-20 13:10:11.372000+ 0100',tz='pytz.FixedOffset(60)')] [Timestamp('2014 -02-04 10:35:47.338000+ 0100',tz='pytz.FixedOffset(60)')]]
per_2_start_time = [[Timestamp('2014 -01-20 13:08:00+ 0100',tz='pytz.FixedOffset(60)')] [Timestamp('2014 -01-20 13:08:00+ 0100',tz='pytz.FixedOffset(60)')]]
per_2_stop_time = [[Timestamp('2014 -01-20 13:09:03.472000+ 0100',tz='pytz.FixedOffset(60)')] [Timestamp('2014 -01-20 13:09:03.472000+ 0100',tz='pytz.FixedOffset(60)')]]
我想找出person 1和person 2在一起的时间(如果有的话)
这就是我尝试的(抱歉这个可怕的语法,我对这个很陌生):
intersept_loc = []
if np.less(per_1_start_time,per_2_start_time).any()& np.greater(per_1_stop_time,per_2_start_time).any():
intersept_loc.append(np.subtract(per_1_stop_time,per_2_start_time))
elif np.greater(per_1_start_time,per_2_start_time).any()& np.greater(per_2_stop_time,per_1_start_time).any():intersept_loc.append(np.subtract(per_2_stop_time,per_1_start_time))elif np.less(per_1_start_time,per_2_start_time).any()& np.greater(per_1_stop_time,per_2_stop_time).any():intersept_loc.append(np.subtract(per_2_stop_time,per_2_start_time))elif np.greater(per_1_start_time,per_2_start_time).any()& np.less(per_1_stop_time,per_2_stop_time).any():intersept_loc.append(np.subtract(per_1_stop_time,per_1_start_time))else:intersept_loc.append(0)
print(intersept_loc3)
这就是我得到的:array([[Timedelta('0 days 00:02:11.372000')],[Timedelta('14 days 21:27:47.338000')]],dtype=object)]第一个时间增量是正确的,但它也在第二行执行功能,这是我不想要的。如果有人能给予我一些指导,我将不胜感激

vfh0ocws

vfh0ocws1#

试试这个,我想会有用的。

from datetime import timedelta

# arrays shoould have the same length
assert len(per_1_start_time) == len(per_1_stop_time) == len(per_2_start_time) == len(per_2_stop_time)

intersect_times = []
for p1_start, p1_stop, p2_start, p2_stop in zip(per_1_start_time, per_1_stop_time, per_2_start_time, per_2_stop_time):
    latest_start = max(p1_start, p2_start)
    earliest_end = min(p1_stop, p2_stop)
    overlap = (earliest_end - latest_start).total_seconds()
    overlap = max(0, overlap)
    intersect_times.append(timedelta(seconds=overlap))

print(intersect_times)

字符串

相关问题