I have a numpy.ndarray. The first two columns are dates of type datetime.datetime. I would like to find the difference in months (as a float) between the two where I define this as (difference in days)*(12/365). So I would like to get access to the .days property of the timedelta objects returned by subtracting two datetimes.
I can’t do this: (MyArray[1] - MyArray[0]).days because Python doesn’t know to expect timedelta objects. In C# I might try casting like this:
((timedelta)(MyArray[1] - MyArray[0])).days
What’s the way to do this in Python (numpy)? Do I need to loop?
Unfortunately, it’s not doable without iterating on your array of
timedeltaHere, we’re using
np.fromiterwith thecountargument to be more efficient (it preallocates the size of the array).Note that the new
datetime64dtypewill not at this date (2012/09) allow you to get properties likedays.