如下面的代码所示,numpy.datetime64的表示在对象无法工作之前成为溢出的牺牲品。
import numpy as np
import datetime
def showMeDifference( t1, t2 ):
dt = t2-t1
dt64_ms = np.array( [ dt ], dtype = "timedelta64[ms]" )[0]
dt64_us = np.array( [ dt ], dtype = "timedelta64[us]" )[0]
dt64_ns = np.array( [ dt ], dtype = "timedelta64[ns]" )[0]
assert( dt64_ms / dt64_ns == 1.0 )
assert( dt64_us / dt64_ms == 1.0 )
assert( dt64_ms / dt64_us == 1.0 )
print str( dt64_ms )
print str( dt64_us )
print str( dt64_ns )
t1 = datetime.datetime( 2014, 4, 1, 12, 0, 0 )
t2 = datetime.datetime( 2014, 4, 1, 12, 0, 1 )
showMeDifference( t1, t2 )
t1 = datetime.datetime( 2014, 4, 1, 12, 0, 0 )
t2 = datetime.datetime( 2014, 4, 1, 12, 1, 0 )
showMeDifference( t1, t2 )
t1 = datetime.datetime( 2014, 4, 1, 12, 0, 0 )
t2 = datetime.datetime( 2014, 4, 1, 13, 0, 0 )
showMeDifference( t1, t2 )
print "These are for " + np.__version__
1000 milliseconds
1000000 microseconds
1000000000 nanoseconds
60000 milliseconds
60000000 microseconds
-129542144 nanoseconds
3600000 milliseconds
-694967296 microseconds
817405952 nanoseconds
These are for 1.7.1
这只是np.timedelta64中的一个错误吗?如果是这样,使用np.timedelta64时人们使用了哪些成语/变通方法?