Numba dictionary is not working

Hi Everyone,

I am completely new to Numba, and I struggle with its implementation of pandas dictionaries.

To play with it I created the code:

import numpy as np

from numba import njit

from numba import types

from numba.typed import Dict

dict_param1 = Dict.empty(

key_type=types.int64,

value_type=types.int64[:],

)

dict_param1[123] = np.asarray([1, 2, 3])

I expected it to run, but instead the following error occured:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function() found for signature:

setitem(DictType[int64,array(int64, 1d, A)]<iv=None>, int64, array(int32, 1d, C))

There are 16 candidate implementations:

  • Of which 14 did not match due to:
    Overload of function ‘setitem’: File: : Line N/A.
    With argument(s): ‘(DictType[int64,array(int64, 1d, A)]<iv=None>, int64, array(int32, 1d, C))’:
    No match.
  • Of which 2 did not match due to:
    Overload in function ‘impl_setitem’: File: numba\typed\dictobject.py: Line 706.
    With argument(s): ‘(DictType[int64,array(int64, 1d, A)]<iv=None>, int64, array(int32, 1d, C))’:
    Rejected as the implementation raised a specific error:
    LoweringError: Failed in nopython mode pipeline (step: native lowering)
    expecting {i8*, i8*, i64, i64, i64*, [1 x i64], [1 x i64]} but got {i8*, i8*, i64, i64, i32*, [1 x i64], [1 x i64]}

File “venv\Lib\site-packages\numba\typed\dictobject.py”, line 716:
def impl(d, key, value):

castedkey = _cast(key, keyty)
castedval = _cast(value, valty)
^

During: lowering “castedval = call $12load_global.4(value, $16load_deref.6, func=$12load_global.4, args=[Var(value, dictobject.py:714), Var($16load_deref.6, dictobject.py:716)], kws=(), vararg=None, varkwarg=None, target=None)” a
t C:\Users\Msit\Documents\venv\Lib\site-packages\numba\typed\dictobject.py (716)
raised from C:\Users\Msit\Documents\venv\Lib\site-packages\numba\core\errors.py:837

During: typing of setitem at C:\Users\Msit\Documents\venv\Lib\site-packages\numba\typed\typeddict.py (34)

File “venv\Lib\site-packages\numba\typed\typeddict.py”, line 34:
def _setitem(d, key, value):
d[key] = value
^
Do you know how should I fix it?
Python version: 3.10
Numba version: 0.57.0
Numpy version: 1.24.2

Hi @Starsky

The error message says that the value you are trying to set is a array(int32, 1d, C) even though you specified array(int64, 1d, A) as the value type. So either change the data type of your array:

dict_param1[123] = np.asarray([1, 2, 3], dtype=np.int32)

or change the value type of the dictionary:

dict_param1 = Dict.empty(
     key_type=types.int64,
     value_type=types.int32[:],
)