On Wed, Jan 20, 2010 at 7:21 AM, Keith Goodman <[email protected]> wrote:
> On Wed, Jan 20, 2010 at 7:11 AM, Keith Goodman <[email protected]> wrote:
>> On Wed, Jan 20, 2010 at 6:01 AM, John [H2O] <[email protected]> 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)
Last one (I promise). If you don't need to keep the dates:
>> A
array([[2010-01-01, 1],
[2010-01-02, 2],
[2010-02-01, 3],
[2010-01-05, 4]], dtype=object)
>> A[:,0] = [date.month for date in A[:,0]]
>>
>> A
array([[1, 1],
[1, 2],
[2, 3],
[1, 4]], dtype=object)
>>
>> A[A[:,0]==1,:]
array([[1, 1],
[1, 2],
[1, 4]], dtype=object)
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion