2015-11-06 31 views
4

我有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 
+0

你可能有一些解释[这里](http://stackoverflow.com/questions/132988/is-there-蟒蛇之间的差异) – MaTh

回答

2

所以也许timedelta为D型np.longdouble没有实现?

总之,是的。

the documentation

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

所有的参数都是可选的,默认为0参数可以是整型,长整型,或漂浮,并且可以是正的或负的。

这里“长”是指一个Python long整数,而不是longdouble浮动。


更新

我想我已经想通的np.float64看似不一致的行为。 似乎是什么相关的是numpy的D型与否的子类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 

然而在评论认为timedelta(np.int64(1))没有使用Python 3.4.3 工作报告的OP。我认为这是因为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 
+0

这真的很奇怪。我以为python是平台独立的。你使用哪种版本的numpy和python?我的:'numpy:1.10.1; Python 3.4.3' –

+0

是的,它似乎是Python3特有的行为。查看我的更新。 –

0

在Anaconda64,Win7的,Python的2.7.11,NumPy的1.10.1,timedelta的回应NumPy的。INT32取决于值:从Spyder的启动

In [3]: datetime.timedelta(seconds = numpy.int32(2147)) 
Out[3]: datetime.timedelta(0, 2147) 

In [4]: datetime.timedelta(seconds = numpy.int32(2148)) 
Out[4]: datetime.timedelta(-1, 84253, 32704) 

完整的日志:

Python 2.7.11 |Anaconda 2.1.0 (64-bit)| (default, Dec 7 2015, 14:10:42) [MSC v.1500 64 bit (AMD64)] 
Type "copyright", "credits" or "license" for more information. 

IPython 4.0.0 -- An enhanced Interactive Python. 
?   -> Introduction and overview of IPython's features. 
%quickref -> Quick reference. 
help  -> Python's own help system. 
object? -> Details about 'object', use 'object??' for extra details. 
%guiref -> A brief reference about the graphical user interface. 
In [1]: import numpy 

In [2]: import datetime 

In [3]: datetime.timedelta(seconds = numpy.int32(2147)) 

Out[3]: datetime.timedelta(0, 2147) 

In [4]: datetime.timedelta(seconds = numpy.int32(2148)) 

Out[4]: datetime.timedelta(-1, 84253, 32704)