question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating 1d array with 0d array fails

See original GitHub issue

Reporting a bug

I tried writing data to an output array that was created in a Numba-compiled function. This failed under some conditions, where a 1d array needed to be populated with a 0d array. A minimal working example is:

@nb.njit
def f(arr):
    out = np.zeros((1,))
    out[0] = arr
    return out

f(np.array(1.))
# expect output: array([1.])

Instead of giving the expected output (which is returned without using numba), I get the following error:

No implementation of function Function(<built-in function setitem>) found for signature:
 
 >>> setitem(array(float64, 1d, C), Literal[int](0), array(float64, 0d, C))

I here use numba version 0.50.1, which I believe is the current one.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
david-zwickercommented, Jul 21, 2020

I ended up writing a small wrapper function that turns 0d arrays into scalars:

@nb.generated_jit
def convert_scalar(arr):
    """ helper function that turns 0d-arrays into scalars """
    if isinstance(arr, nb.types.Array) and arr.ndim == 0:
        return lambda arr: arr[()]
    else:
        return lambda arr: arr

This helps to avoid the bug, but is obviously not a long-term solution.

0reactions
stuartarchibaldcommented, Sep 20, 2022

Thanks for closing @david-zwicker!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python, numpy; How to best deal with possible 0d arrays
I use the numpy.asarray function to force the inputs into the form I want, conveniently with no duplication for things that are already...
Read more >
the absolute basics for beginners — NumPy v1.25.dev0 Manual
How to convert a 1D array into a 2D array (how to add a new axis to an array)#. This section covers np.newaxis...
Read more >
NumPy Creating Arrays - W3Schools
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. These are the most common and basic arrays....
Read more >
NumPy array reshape (Shape transformation without data ...
The method reshape() will return a reshaped array with the same data. Reshape 1d to 2d. To convert 1D array to 2D array,...
Read more >
NumPy: zeros() function - w3resource
NumPy array creation: zeros() function, example - Return a new array of given shape and type, filled with zeros.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found