我有dtype numpy.longdouble
时,当我试图使用该值与timedelta
函数我有错误。但是,当我将它转换为numpy.float64
一切都很好。有人可以解释这种行为吗?timedelta错误与numpy.longdouble dtype
import numpy as np
from datetime import timedelta
t1 = np.array([1000], dtype=np.longdouble)
t2 = np.array([1000], dtype=np.float64)
In [166]: timedelta(seconds=t1[0])
TypeError: unsupported type for timedelta seconds component: numpy.float64
In [167]: timedelta(seconds=t2[0])
Out[167]: datetime.timedelta(0, 1000)
In [168]: timedelta(seconds=t1[0].astype(np.float64))
Out[168]: datetime.timedelta(0, 1000)
当我想看看他们是看类似的变量dtypes,但不一样的:
In [172]: t1[0].dtype
Out[172]: dtype('float64')
In [173]: t2[0].dtype
Out[173]: dtype('float64')
In [174]: np.longdouble == np.float64
Out[174]: False
In [175]: t1[0].dtype == t2[0].dtype
Out[175]: True
编辑
而且这奇怪的是,它不工作的np.int32和np.int64之一:
t3 = np.array([1000], dtype=np.int32)
t4 = np.array([1000], dtype=np.int64)
In [29]: timedelta(t3[0])
TypeError: unsupported type for timedelta days component: numpy.int32
In [69]: timedelta(t4[0])
TypeError: unsupported type for timedelta days component: numpy.int64
你可能有一些解释[这里](http://stackoverflow.com/questions/132988/is-there-蟒蛇之间的差异) – MaTh