python - numpy.longdouble dtype 的 timedelta 错误

标签 python numpy types 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)

当我试图查看变量的数据类型时,它们看起来相似但不相同:

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

最佳答案

So maybe timedelta for dtype np.longdouble isn't implemented?

简而言之,是的。

来自 the documentation :

class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

All arguments are optional and default to 0. Arguments may be ints, longs, or floats, and may be positive or negative.

这里 “long” 指的是 Python long 整数,而不是 longdouble float 。


更新

我想我已经理解了 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

但是 OP 在评论中报告说 timedelta(np.int64(1)) 使用 Python 3.4.3 工作。我认为这是因为 when numpy is built on Python 3x, np.int64 no longer subclasses 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

关于python - numpy.longdouble dtype 的 timedelta 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33566939/

相关文章:

mysql - 将字符串构建的位图 (100101) 转换为 SQL 中的 int 值

python - 给定一个按字典顺序排列的元素列表(即 ['a' 、 'b' 、 'c' 、 'd' ]),找出第 n 个排列 - 求解的平均时间?

python - Pandas :带有 np.seterr(所有 ='raise')和缺失数据的 FloatingPointError

python - Django : Timeout when reading response headers from daemon process 的 mod_wsgi 错误

python - Numpy矩阵行列式精度问题

Python 创建类的多个实例

C#:将字符串转换为对象引用

haskell - 函数依赖/类型族

Python dask 延迟迭代series.unique()值

python - Kivy:从任何小部件访问配置值