带有dtype十进制的numpy数组? [英] numpy array with dtype Decimal?

查看:69
本文介绍了带有dtype十进制的numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小数dtype是否在numpy中可用?

Are Decimal dtypes available in numpy?

>>> import decimal, numpy
>>> d = decimal.Decimal('1.1') 
>>> s = [['123.123','23'],['2323.212','123123.21312']]
>>> ss = numpy.array(s, dtype=numpy.dtype(decimal.Decimal))
>>> a = numpy.array(s, dtype=float)
>>> type(d)
<class 'decimal.Decimal'>
>>> type(ss[1,1])
<class 'str'>
>>> type(a[1,1])
<class 'numpy.float64'>

我认为numpy.array不支持每个dtype,但我认为只要定义正确的操作,它至少可以使dtype传播到尽可能远的距离.我想念什么吗?有什么办法可以解决这个问题吗?

I suppose numpy.array doesn't support every dtype, but I sort of thought that it would at least let a dtype propagate as far as it could as long as the right operations were defined. Am I missing something? Is there some way for this to work?

推荐答案

重要提示:这是一个糟糕的答案

所以我在真正理解问题之前回答了这个问题.答案已被接受,并且有一些赞同,但您最好跳至下一个.

So I answered this question before I really understood the point of it. The answer was accepted, and has some upvotes, but you would probably do best to skip to the next one.

原始答案:

Decimal似乎可用:

>>> import decimal, numpy
>>> d = decimal.Decimal('1.1')
>>> a = numpy.array([d,d,d],dtype=numpy.dtype(decimal.Decimal))
>>> type(a[1])
<class 'decimal.Decimal'>

我不确定您要完成的工作,您的示例比简单创建十进制numpy数组所需的示例复杂得多.

I'm not sure exactly what you are trying to accomplish, your example is more complicated than is necessary for simply creating a decimal numpy array.

这篇关于带有dtype十进制的numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆