[Numpy-discussion] Help with np.where and datetime functions
Pierre GM
pgmdevlist@gmail....
Wed Jul 8 13:44:50 CDT 2009
On Jul 8, 2009, at 7:03 AM, John [H2O] wrote:
>
> Hello,
>
> I have several issues which require me to iterate through a fairly
> large
> array (300000+ records).
>
> The first case is calculating and hourly average from non-regularly
> sampled
> data.
Would you like to give the scikits.timeseries package a try ? It's
available at pytseries.sourceforge.net.
Calculatng the hourly average should be straightforward.
> The second is screening one array, based on data in the second array.
> The functions are defined below, but inherent to each is the following
> snippet:
>
> ind = np.where( (t1 < X[:,0]) & (X[:,0] < t2) )
>
> where X is a (n,2) array and X[:,0] = a vector of datetime objects.
>
> What I am trying to do (obviously?) is find all the values of X that
> fall
> within a time range.
Well, timeseries could help you on this one as well.
>
> Specifically, one point I do not understand is why the following two
> methods
> fail:
>
> --> 196 ind = np.where( (t1 < Y[:,0] < t2) ) #same result
> with/without inner parens
> TypeError: can't compare datetime.datetime to numpy.ndarray
>
>
> OR trying the 'and' method:
>
> --> 196 ind = np.where( (Y[:,0]>t1) and (Y[:,0]<t2) )
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()
As mentioned by Neil, you need to use & instead of and in your
expression. You can also use the lengthier np.logical_and(Y[:,0]>t1,
Y[:,0]<t2)
Also, using np.where without additional inputs
(np.where(condition,x,y)) is equivalent to .nonzero
More information about the NumPy-Discussion
mailing list