[Python] Why keyword argument 'weights' doesn't work when calling NumPy histogram in Numba?

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    This Python 3.13.5 script with numpy 2.2.6 and numba 0.61.2:

    import numpy as np, numba as nb

    @nb.njit(fastmath=True)
    def f(a, b):
    return np.histogram(a, 10, weights=b)

    a = np.random.randint(0, 256, (100,)).astype(np.uint8)
    b = np.random.randint(0, 256, (100,)).astype(np.uint8)

    print(np.histogram(a, 10, weights=b)) # no problems
    print(f(a, b)) # fails here


    fails with this error:

    numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
    No implementation of function Function(<function histogram at 0x7244459565c0>) found for signature:

    >>> histogram(array(uint8, 1d, C), Literal[int](10), weights=array(uint8, 1d, C))

    There are 2 candidate implementations:
    - Of which 2 did not match due to:
    Overload in function 'np_histogram': File: numba/np/old_arraymath.py: Line 3942.
    With argument(s): '(array(uint8, 1d, C), int64, weights=array(uint8, 1d, C))':
    Rejected as the implementation raised a specific error:
    TypingError: got an unexpected keyword argument 'weights'
    raised from /home/paul/st-python/test-3.13/.venv/lib/python3.13/site-packages/numba/core/typing/templates.py:791

    During: resolving callee type: Function(<function histogram at 0x7244459565c0>)
    During: typing of call at /home/paul/st-python/test-3.13/so-numba-hist.py (5)


    File "so-numba-hist.py", line 5:
    def f(a, b):
    return np.histogram(a, 10, weights=b)
    ^

    During: Pass nopython_type_inference


    Why Numba doesn't accept a legal weights= argument? BTW, this is just a shortened example, my use case is a longer function, which calls np.histogram().

    Continue reading...

Compartilhe esta Página