[Numpy-discussion] Accessing fields of the object stored in numpy array

Vishal Rana ranavishal@gmail....
Thu Feb 18 11:04:52 CST 2010


Thanks Friedrich it helped.

On Thu, Feb 18, 2010 at 2:45 AM, Friedrich Romstedt <
friedrichromstedt@gmail.com> wrote:

> Hello Vishal,
>
> 2010/2/18 Vishal Rana <ranavishal@gmail.com>:
>
> > a = np.array([dt.datetime(2010, 2, 17), dt.datetime(2010, 2, 16),
> > dt.datetime(2010, 2, 15)])
> > b = np.array([dt.datetime(2010, 2, 14), dt.datetime(2010, 2, 13),
> > dt.datetime(2010, 2, 12)])
>
> > c=a-b
> > c.days (a numpy array of days difference) like:
> > array([3, 3, 3])
>
> I think a (rather slow) solution would be to use:
>
> def days(timedelta):
>    return timedelta.days
>
> udays = numpy.vectorize(days)
>
> and applying the ufunc udays() on your dtype = numpy.object array like:
>
> c_days = udays(c)
>
> numpy.vectorize() turns an ordinary function into an ufunc.  This
> means, that the ufunc created can take ndarrays and the days()
> function will be applied to all elements.
>
> hth,
> Friedrich
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100218/bd0ce748/attachment.html 


More information about the NumPy-Discussion mailing list