Yun Wei
Yun Wei

Reputation: 259

How to specify the 'string' data type when using Numba?

Numba does not recognize the string. How can I correct the following code? Thank you!

@nb.jit(nb.float64(nb.float64[:], nb.char[:]), nopython=True, cache=True)
def func(x, y='cont'):
    """
    :param x: is np.array, x.shape=(n,)
    :param y: is a string, 
    :return: a np.array of same shape as x
    """
    return result

Upvotes: 8

Views: 6779

Answers (1)

JoshAdel
JoshAdel

Reputation: 68722

The following works for Numba 0.44:

import numpy as np
import numba as nb

from numba import types

@nb.jit(nb.float64[:](nb.float64[:], types.unicode_type), nopython=True, cache=True)
def func(x, y='cont'):
    """
    :param x: is np.array, x.shape=(n,)
    :param y: is a string, 
    :return: a np.array of same shape as x
    """
    print(y)
    return x

However, you're going to get an error if you try to run func with no value of y specified, since in your signature you're saying that the second argument is required. I tried figuring out how to handle the optional parameter (looked at types.Omitted), but couldn't quite figure it out. I would possibly look at not specifying a signature and leaving numba to do the proper type inference:

@nb.jit(nopython=True, cache=True)
def func2(x, y='cont'):
    """
    :param x: is np.array, x.shape=(n,)
    :param y: is a string, 
    :return: a np.array of same shape as x
    """
    print(y)
    return x

Upvotes: 6

Related Questions