convert float to string numba python numpy array

Cha*_*apo 5 python string floating-point numpy numba

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

I am getting the following error :

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

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

Jac*_*din 4

numpy.str目前还不支持该功能。Numba 的网站上提供了所有支持的numpy功能的列表。

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

完成您正在尝试的操作的唯一方法是以某种方式创建一个将浮点数转换为字符串的函数,仅使用 Numba 支持的 Python 和 Numpy 的功能。

在朝这个方向前进之前,我仍然会重新考虑将浮点数转换为字符串的必要性。它可能不是很有效,并且由于将浮点数转换为字符串,您可能会因为增加一些开销而失去抖动一些函数的好处。

当然,如果不了解更多有关该项目的信息,这很难说。