Search code examples
pythonnumba

int64 vs array(int64, 0d, C) in numba


I define a jited function returning a tuple using numba. It's something like below.

import numba as nb
from numba.types import Tuple
import numpy as np

FOO_T = Tuple.from_types((nb.types.NPDatetime('M'), nb.int64))

@nb.jit([FOO_T(nb.types.NPDatetime('M'), nb.types.NPDatetime('M'))], nopython=True)
def foo(input1, input2):
    temp1 = input1
    temp2 = np.array(input1 - input2).view(nb.int64)
    
    output = (temp1, temp2)
    
    return output

A TypeError is reported as below. The second element of output tuple is defined as int64. However, it's actually compiled as array(int64, 0d, C).

TypingError: No conversion from Tuple(datetime64[M], array(int64, 0d, C)) to Tuple(datetime64[M], int64) for '$38return_value.15', defined at None

Have no idea how to make them consistent. Thanks for your help.


Solution

  • np.array(input1 - input2).view(nb.int64) is an array of int64 and not a scalar. This is why Numba report an error. Note that np.array(input1 - input2) results in a weird type: an array of dimension 0. AFAIK, this is what Numpy use to represent scalars but such an array cannot be indexed in Numba nor converted to a scalar.

    You could subtract two scalar and build an array with np.array([input1 - input2]) and then call view. That being said, view is probably not what you want to do here as it reinterpret the binary representation of a NPDatetime as an integer. This is really unsafe and AFAIK there is no reason to assume that this can work. You can just make the difference and cast the result with (np.uint64)(input1 - input2).