【发布时间】:2012-01-09 15:19:22
【问题描述】:
我在数组中有许多时间序列数据,并希望以尽可能避免循环的最简单方式提取给定日期之间的值。 这是一个例子:
from numpy import *
from datetime import *
# datetime array
date_a=array([
datetime(2000,1,1),
datetime(2000,1,2),
datetime(2000,1,3),
datetime(2000,1,4),
datetime(2000,1,5),
])
# item array, indices corresponding to datetime array
item_a=array([1,2,3,4,5])
# extract items in a certain date range
# after a certain date, works fine
item_b=item_a[date_a >= (datetime(2000,1,3))] #Out: array([3, 4, 5])
# between dates ?
item_c=item_a[date_a >= (datetime(2000,1,3)) and date_a <= (datetime(2000,1,4))]
# returns: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
这个问题有单线解决方案吗?我查看了 numpy any() 和 all() 以及 where(),但无法找到解决方案。我感谢任何帮助和指示!
【问题讨论】:
标签: python indexing numpy slice