【发布时间】:2016-01-13 06:22:17
【问题描述】:
我有两个np.datetime64 格式的日期时间列表。 (不需要 - 可以是 unix 时间戳或 datetime.datetime)
当我遍历密集列表 (times_dense) 时,我希望从 times_sparse 获得的时间最接近但小于来自 times_dense 的时间。我在datetime 很糟糕,所以我把它放在一起。
most_recent_time = None
for time_d in times_dense:
for time_s in times_sparse:
# time_d is after time_s and time_s is after most_recent_time
if(time_d >= time_s and time_s > most_recent_time):
most_recent_time = time_s
return most_recent_time
有没有简单的方法来做到这一点?我的方法会奏效吗?它很笨重并且运行时间很长。解决这个问题的最佳方法是什么?
PS。我最初在 pandas 数据框中有这些,但因为在数据框中找不到解决方案而将它们取出。如果这可以与 pandas 一起使用,那就更好了。
【问题讨论】:
-
times_dense和times_sparse到底是什么? Python 列表、numpy 数组、scipy 稀疏矩阵或您自己制作的一些可迭代对象? -
现在两者都是 np 数组。但它们可以是列表或 pandas 数据框中。
-
most_recent_time 的重要性是什么?此外,当您说您希望 time_sparse 中最接近 time_dense 的时间时,您是针对 time_dense 中的特定给定元素,还是希望找到一对时间元素,一个在 time_sparse 中,另一个在 time_dense 中,它们具有最小值与其他可能的对相比的距离?
-
most_recent_time看起来就像一个临时值,用于迭代地找到最大值(或最小值)。
标签: python datetime numpy pandas timestamp