I have a structured array and some of its fields may or may not exist. I would like to check whether it exists and do something based on it, like the following:
import numpy as np
from numba import njit
from numba.extending import overload
def get_keys(recarr):
return tuple(recarr.dtype.fields.keys())
@overload(get_keys)
def ol_get_keys(recarr):
keys = get_keys(recarr)
return lambda recarr: keys
INFO = np.void(
(np.nan,),
dtype=np.dtype(
[('a', np.float64)],
align=True,
),
)
info = np.full(1, INFO)
@njit
def test(info):
n = 0
if 'b' in get_keys(info):
n += info[0]['b']
return n
test(info)
But this is not working:
KeyError: "Field 'b' was not found in record with fields ('a',)"
Anyone has ideas? Thanks in advance!