numpy.longdouble dtype的timedelta错误

Ant*_*pov 9 python types numpy timedelta python-datetime

我有时间使用dtype numpy.longdouble,当我试图将这些值与timedelta函数一起使用时,我遇到了错误.但是当我把它转换成numpy.float64一切都很好.有人可以解释一下这种行为吗?

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)
Run Code Online (Sandbox Code Playgroud)

当我试图看到变量的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
Run Code Online (Sandbox Code Playgroud)

编辑

奇怪的是,它不适用于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
Run Code Online (Sandbox Code Playgroud)

ali*_*i_m 7

所以也许timedelta对于 dtypenp.longdouble没有实现?

简而言之,是的。

文档

datetime.timedelta([[,[,微秒[,毫秒[,分钟[,小时[,]]]]]]]])

所有参数都是可选的,默认为0. 参数可以是 ints、longs 或 floats,并且可以是正数或负数。

这里的“long”指的是 Pythonlong整数,而不是longdouble浮点数。


更新

我想我已经发现np.float64. 似乎相关的是 numpy dtype 是否是timedelta接受的本机 Python 标量类型之一的子类。

在我的 64 位机器上,运行 Python 2.7.9,numpy v1.10.1:

In [1]: timedelta(np.float64(1))
Out[1]: datetime.timedelta(1)

In [2]: timedelta(np.float32(1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4a7874ba393b> in <module>()
----> 1 timedelta(np.float32(1))

TypeError: unsupported type for timedelta days component: numpy.float32

In [3]: timedelta(np.int64(1))
Out[3]: datetime.timedelta(1)

In [4]: timedelta(np.int32(1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-0475c6c8f1aa> in <module>()
----> 1 timedelta(np.int32(1))

TypeError: unsupported type for timedelta days component: numpy.int32

In [5]: issubclass(np.float64, float)
Out[5]: True

In [6]: issubclass(np.float32, float)
Out[6]: False

In [7]: issubclass(np.int64, int)
Out[7]: True

In [8]: issubclass(np.int32, int)
Out[8]: False
Run Code Online (Sandbox Code Playgroud)

然而,OP报道,该意见timedelta(np.int64(1))没有使用Python 3.4.3工作。我认为这是因为当 numpy 建立在 Python 3x 上时,np.int64不再是int.

以下是 Python 3.4.3 中发生的事情:

In [1]: timedelta(np.int64(1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-9902ea26a52d> in <module>()
----> 1 timedelta(np.int64(1))

TypeError: unsupported type for timedelta days component: numpy.int64

In [2]: issubclass(np.int64, int)
Out[2]: False
Run Code Online (Sandbox Code Playgroud)


tas*_*ash 7

我遇到了类似的问题

timedelta(days = value.astype(int))没有用,但是

timedelta(days = int(value))工作了。

我希望它对未来的 Google 员工有用。