Passing a list of numpy arrays into np.array with numba

Hi all,

I’m trying to create an np.array from a list of np.arrays,

arr = np.array([a,b,c])

where a, b and c are 1D numpy arrays.

This throws a TypingsError and I’ve tried to declare the signature type as:

@jit(types.Array(types.float64, 2, 'C')(types.List(types.Array(types.float64, 1, 'C'))), nopython=True)
def make_np_arr(x):
    return np.array(x)

But I’m still getting an error like so:

There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'array': File: numba/core/typing/npydecl.py: Line 482.
    With argument(s): '(list(array(float64, 1d, C))<iv=None>)':
   Rejected as the implementation raised a specific error:
     TypingError: array(float64, 1d, C) not allowed in a homogeneous sequence
  raised from /Users/briant/opt/anaconda3/envs/seam-carving/lib/python3.7/site-packages/numba/core/typing/npydecl.py:449

I’ve tried to overload np.array as well with the below, to no avail (adapted from here):

@overload(np.array)
def np_array_ol(x):
    if (isinstance(x, types.List(types.Array(types.float64, 1, 'C'))):
        def impl(x):
            return np.copy(x)
        return impl

Thanks,
Brian

Hi @brian,

You might be able to use this workaround:

from numba import njit
import numpy as np

@njit
def make_2d(arraylist):
    n = len(arraylist)
    k = arraylist[0].shape[0]
    a2d = np.zeros((n, k))
    for i in range(n):
        a2d[i] = arraylist[i]
    return(a2d)

a = np.array((0, 1, 2, 3))
b = np.array((4, 5, 6, 7))
c = np.array((9, 10, 11, 12))

make_2d([a, b, c])

array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  6.,  7.],
       [ 9., 10., 11., 12.]])

Works like a charm, thanks @ryanchien!

Your answer helped me with many other functions as well as it made me think in terms of rewriting whole numpy functions for my specific use-cases to take advantage of numba.

Thanks again!

1 Like