将float转换为字符串numba python numpy数组 [英] convert float to string numba python numpy array

查看:868
本文介绍了将float转换为字符串numba python numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行@nb.njit函数,试图在其中将整数放入字符串数组中.

I am running a @nb.njit function within which I am trying to put an integer within a string array.

import numpy as np
import numba as nb

@nb.njit(nogil=True)
def func():
    my_array = np.empty(6, dtype=np.dtype("U20"))
    my_array[0] = np.str(2.35646)
    return my_array


if __name__ == '__main__':
    a = func()
    print(a)

我遇到以下错误:

numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<class 'str'>) with argument(s) of type(s): (float64)

我应该使用哪个函数在numba中从float转换为string?

Which function am I supposed to use to do the conversion from float to string within numba ?

推荐答案

到目前为止,尚不支持numpy.str函数.提供了所有支持的numpy函数的列表.在Numba的网站上.

The numpy.str function is not supported so far. A list of all the supported numpy functions is available on Numba's website.

也不支持内置的str.可以在支持的Python功能页面上进行检查.

The built-in str is not supported either. This can be checked on the supported Python features page.

执行尝试操作的唯一方法是仅使用Numba支持的Python和Numpy功能,通过某种方式使函数将浮点数转换为字符串.

The only way to do what you are trying would be to somehow make a function that converts a float to a string, using only the features of Python and Numpy supported by Numba.

在朝这个方向前进之前,我将重新考虑将浮点数转换为字符串的必要性.它可能不是很有效,并且由于将float转换为string而增加了一些开销,因此您可能会失去通过添加一些功能而失去的优势.

Before going in this direction, I would nevertheless reconsider the necessity to convert floats into strings. It may not be very efficient and you may lose the benefit of jitting a few functions by adding some overhead due to the conversion of floats to string.

当然,如果不进一步了解该项目,这很难说.

Of course, this is hard to tell without knowing more about the project.

这篇关于将float转换为字符串numba python numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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