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

Friedrich Romstedt friedrichromstedt at gmail.com
Thu Feb 18 05:45:10 EST 2010


Hello Vishal,

2010/2/18 Vishal Rana <ranavishal at 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



More information about the NumPy-Discussion mailing list