[Numpy-discussion] Changing the datetime operation unit rules

Dave Hirschfeld dave.hirschfeld@gmail....
Tue Jun 7 16:47:24 CDT 2011


Mark Wiebe <mwwiebe <at> gmail.com> writes:

> 
> >>> a = np.datetime64('today')
> 
> >>> a - a.astype('M8[Y]')
> 
> numpy.timedelta64(157,'D')
> 
> vs
> 
> 
> >>> a = np.datetime64('today')
> >>> a - a.astype('M8[Y]')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: ufunc subtract cannot use operands with types
dtype('datetime64[D]') and dtype('datetime64[Y]')
> 
> 
> Cheers,
> Mark
> 
> 

I think I like the latter behaviour actually. The first one implicitly assumes 
I want the yearly frequency converted to the start of the period. I think the 
user should have to explicitly convert the dates to a common representation 
before performing arithmetic on them.

It seems to me that if you didn't want your data to be silently cast to the
start of the period it would be very difficult to determine if this had 
happened or not.

As an example consider the pricing of a stock using the black-scholes formula.
The value is dependant on the time to maturity which is the expiry date minus
the current date. If the expiry date was accidentally specified at (or converted
to) a monthly frequency and the current date was a daily frequency taking the
difference will give you the wrong number of days until expiry resulting in a
subtly different answer - not good.

NB: The timeseries Date/DateArray have a day_of_year attribute which is very 
useful.

Regards,
Dave







More information about the NumPy-Discussion mailing list