A[:, [1]] not supported where A[:, 1:1+1] is

The following numpy code is not supported in numba.

@nb.njit
def foo(A):
    return A[:, [1]]

A = np.ones((3, 3))
foo(A)

However it is supported if we use:

@nb.njit
def foo(A):
    return A[:, 1:1+1]

There is an issue for this but it has no comments. What is the explanation for this?

Hey @lesshaste,

Slicing (A[:, 1:1+1]) works because it is supported in your context.
Advanced indexing with a list (A[:, [1]]) isn’t supported because Numba allows only one advanced index, and it must be a one-dimensional array.
Therefore, the list [1] triggers the error.

`TypingError: No implementation of function Function(<built-in function getitem>) found for signature:`
getitem(array(float64, 2d, C), Tuple(slice<a:b>, list(int64)<iv=[1]>))

For more details:
https://numba.readthedocs.io/en/stable/reference/numpysupported.html#array-access

1 Like

Does this mean 1:2 is not a list? Apologies for my python ignorance.

This is a slice.

import numpy as np
np.s_[1:2]
# slice(1, 2, None)

You can see it in the error message, too. The first argument in A[:, [1]] is recognized as a slice, the second is a list.

getitem(array(float64, 2d, C), Tuple(slice<a:b>, list(int64)<iv=[1]>))

Slicing is implemented in numba-main/numba/cpython/slicing.py.
Advanced indexing doesn’t seem to be completely supported.

1 Like