Extract items from array: between given values/conditions

我的未来我决定 提交于 2019-12-25 02:43:13

问题


I have a number of timeseries data in arrays and wish to extract values between given dates in the simplest way possible avoiding loops. Here's an example:

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()

Is there a one-line solution to this? I have looked at numpy any() and all(), and also where(), without being able to find a solution. I appreciate any help and point-in-direction!


回答1:


If you want one-liner, then you can use

item_c=item_a[(date_a >= (datetime(2000,1,3))) * (date_a <= (datetime(2000,1,4)))]



回答2:


It's not clear to me why you are using the item_a variable. But to isolate the entries you want you can simply do:

>>> np.where(np.logical_and(date_a >= datetime(2000,1,3), date_a <= datetime(2000,1,4)))
(array([2, 3]),)

The resulting indexes are zero-based, so they correspond to the third and fourth element of your array.

EDIT: np is due to import numpy as np. Doing from numpy import * is in fact a very bad idea. You will overwrite built in functions such as sum and abs for example...

HTH!




回答3:


I think the following should work for you using List Comprehension

[item_a[i] for i in xrange(0,len(date_a)) if date_a[i] >= (datetime(2000,1,3)) and date_a[i] <= (datetime(2000,1,4))]

Select all items in item_a within range 0 <= i < length of date_a where datetime(2000,1,3) <= date_a[i] <= datetime(2000,1,4)



来源:https://stackoverflow.com/questions/8325089/extract-items-from-array-between-given-values-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!