[Numpy-discussion] dates, np.where finding months
Keith Goodman
kwgoodman@gmail....
Wed Jan 20 09:21:53 CST 2010
On Wed, Jan 20, 2010 at 7:11 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
> On Wed, Jan 20, 2010 at 6:01 AM, John [H2O] <washakie@gmail.com> wrote:
>>
>> I have an array with the leading column a series of datetime objects. It
>> covers several years. What is the most efficient way to pull out all the
>> 'January' dates?
>>
>> Right now I do this:
>>
>> A = array with column 0 datetime objects
>>
>> January = [i for i in A if i[0].month ==1 ]
>>
>> It works, but I would rather use np.where and get indices and not have to
>> convert my list back into an array.
>
> Instead of doing this:
>
>>> A
>
> array([[2010-01-01, 1],
> [2010-01-02, 2],
> [2010-02-01, 3],
> [2010-01-05, 4]], dtype=object)
>
>>> [i for i in A if i[0].month==1]
>
> [array([2010-01-01, 1], dtype=object),
> array([2010-01-02, 2], dtype=object),
> array([2010-01-05, 4], dtype=object)]
>
> You could do this:
>
>>> [i for i, date in enumerate(A[:,0]) if date.month==1]
> [0, 1, 3]
Or maybe this is cleaner:
>> [date.month==1 for date in A[:,0]]
[True, True, False, True]
which can be used like this:
>> idx = np.array([date.month==1 for date in A[:,0]])
>> A[idx,:]
array([[2010-01-01, 1],
[2010-01-02, 2],
[2010-01-05, 4]], dtype=object)
More information about the NumPy-Discussion
mailing list