[Numpy-discussion] converting array of timedelta to array of integers.

Keith Goodman kwgoodman at gmail.com
Tue Aug 26 15:03:31 EDT 2008


On Tue, Aug 26, 2008 at 11:47 AM, Keith Goodman <kwgoodman at gmail.com> wrote:
> On Tue, Aug 26, 2008 at 11:23 AM, Ryan Neve <ryan.neve at gmail.com> wrote:
>> Apologies in advance if this is an obvious answer, but I'm new to most of
>> this.
>>
>> My overall goal is to produce a contour plot of some irregular time series
>> data.
>>
>> I've imported the data from mySQL into three arrays x,y,and z where x is an
>> array of datetime.timedelta objects.
>>
>> I need an array of the values of x converted into an array of integers
>> representing the number of minutes in the timedelta.
>>
>> I've tried a few things, and spent hours searching for examples, but to no
>> avail.
>>
>> Once I get the x array as integers, I can use griddata()
>
> I got stuck:
>
>>> import numpy as np
>>> import datetime
>>>
>>> x = np.array([datetime.timedelta(1), datetime.timedelta(2)])
>>> def convert(x):
>   ...:     return x.days * 24.0 * 60 + x.seconds / 60.0 +
> x.microseconds / 6000.0
>   ...:
>>> convert(x[0])
>   1440.0
>>> convert(x[1])
>   2880.0
>>> np.apply_along_axis(convert, 0, x)
> ---------------------------------------------------------------------------
> AttributeError: 'numpy.ndarray' object has no attribute 'days'

This is the first time I've used vectorize. Seems to work:

>> vconvert = np.vectorize(convert)
>> vconvert(x)
   array([ 1440.,  2880.])



More information about the NumPy-Discussion mailing list