Getting 'unsupported array index type unicode_type' error when selecting a column based on condition in Numba with NumPy structured array

huangapple go评论74阅读模式
英文:

Getting 'unsupported array index type unicode_type' error when selecting a column based on condition in Numba with NumPy structured array

问题

以下是您要翻译的内容:

我正在尝试选择一个结构化的 NumPy 数组的列。要选择的列取决于将传递给函数的条件。当我尝试基于条件选择列名时,Numba 抛出下面的错误。否则一切都运行正常。

基本示例:
这个有效:

import numpy as np
import numba

@numba.njit(fastmath=True, cache=True)
def fun(a, b=0):
    c = 'name'
    #if b:
    #    c = 'age'
    return a[c]

a = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
             dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
             
fun(a)

这个不起作用:

import numpy as np
import numba

@numba.njit(fastmath=True, cache=True)
def fun(a, b=0):
    c = 'name'
    if b:
        c = 'age'
    return a[c]

a = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
             dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
             
fun(a)

错误信息:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
c:\Users\User\Workspaces\temp.ipynb Cell 9 in ()
      8     return a[c]
     10 a = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
     11              dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
---> 13 fun(a)

File ~\AppData\Roaming\Python\Python38\site-packages\numba\core\dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
    464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
    465                f"by the following argument(s):\n{args_str}\n")
    466         e.patch_message(msg)
--> 468     error_rewrite(e, 'typing')
    469 except errors.UnsupportedError as e:
    470     # Something unsupported is present in the user code, add help info
    471     error_rewrite(e, 'unsupported_error')

File ~\AppData\Roaming\Python\Python38\site-packages\numba\core\dispatcher.py:409, in _DispatcherBase._compile_for_args..error_rewrite(e, issue_type)
    407     raise e
    408 else:
--> 409     raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function() found for signature:
 
 >>> getitem(unaligned array(Record(name[type=[unichr x 10];offset=0],age[type=int32;offset=40],weight[type=float32;offset=44];48;False), 1d, C), unicode_type)
 
There are 22 candidate implementations:
      - Of which 20 did not match due to:
      Overload of function 'getitem': File: : Line N/A.
        With argument(s): '(unaligned array(Record(name[type=[unichr x 10];offset=0],age[type=int32;offset=40],weight[type=float32;offset=44];48;False), 1d, C), unicode_type)'
       No match.
      - Of which 2 did not match due to:
      Overload in function 'GetItemBuffer.generic': File: numba\core\typing\arraydecl.py: Line 166.
        With argument(s): '(unaligned array(Record(name[type=[unichr x 10];offset=0],age[type=int32;offset=40],weight[type=float32;offset=44];48;False), 1d, C), unicode_type)':
       Rejected as the implementation raised a specific error:
         NumbaTypeError: unsupported array index type unicode_type in [unicode_type]
  raised from C:\Users\User\AppData\Roaming\Python\Python38\site-packages\numba\core\typing\arraydecl.py:72

During: typing of intrinsic-call at C:\Users\User\AppData\Local\Temp\ipykernel_37200\1621110578.py (8)

File "......\AppData\Local\Temp\ipykernel_37200\1621110578.py", line 8:
英文:

I am trying to select a column of a structured NumPy array. The column to be selected depends on a condition that will be passed to the function. Numba throws the error below when I try to select the column name based on the condition. Otherwise everything works well.

Basic example:
This works:

import numpy as np
import numba

@numba.njit(fastmath=True, cache=True)
def fun(a, b=0):
    c = 'name'
    #if b:
    #    c = 'age'
    return a[c]

a = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
             dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
             
fun(a)

This doesn't work:

import numpy as np
import numba

@numba.njit(fastmath=True, cache=True)
def fun(a, b=0):
    c = 'name'
    if b:
        c = 'age'
    return a[c]

a = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
             dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
             
fun(a)

The error:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
c:\Users\User\Workspaces\temp.ipynb Cell 9 in ()
8     return a[c]
10 a = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
11              dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
---> 13 fun(a)
File ~\AppData\Roaming\Python\Python38\site-packages\numba\core\dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
465                f"by the following argument(s):\n{args_str}\n")
466         e.patch_message(msg)
--> 468     error_rewrite(e, 'typing')
469 except errors.UnsupportedError as e:
470     # Something unsupported is present in the user code, add help info
471     error_rewrite(e, 'unsupported_error')
File ~\AppData\Roaming\Python\Python38\site-packages\numba\core\dispatcher.py:409, in _DispatcherBase._compile_for_args..error_rewrite(e, issue_type)
407     raise e
408 else:
--> 409     raise e.with_traceback(None)
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function() found for signature:
>>> getitem(unaligned array(Record(name[type=[unichr x 10];offset=0],age[type=int32;offset=40],weight[type=float32;offset=44];48;False), 1d, C), unicode_type)
There are 22 candidate implementations:
- Of which 20 did not match due to:
Overload of function 'getitem': File: : Line N/A.
With argument(s): '(unaligned array(Record(name[type=[unichr x 10];offset=0],age[type=int32;offset=40],weight[type=float32;offset=44];48;False), 1d, C), unicode_type)':
No match.
- Of which 2 did not match due to:
Overload in function 'GetItemBuffer.generic': File: numba\core\typing\arraydecl.py: Line 166.
With argument(s): '(unaligned array(Record(name[type=[unichr x 10];offset=0],age[type=int32;offset=40],weight[type=float32;offset=44];48;False), 1d, C), unicode_type)':
Rejected as the implementation raised a specific error:
NumbaTypeError: unsupported array index type unicode_type in [unicode_type]
raised from C:\Users\User\AppData\Roaming\Python\Python38\site-packages\numba\core\typing\arraydecl.py:72
During: typing of intrinsic-call at C:\Users\User\AppData\Local\Temp\ipykernel_37200\1621110578.py (8)
File "..\..\..\..\..\AppData\Local\Temp\ipykernel_37200\1621110578.py", line 8:

答案1

得分: 1

看起来当数组索引在编译时是未知的时候,它似乎不太满意/不支持。

除了这个例子之外,你需要其他解决方案吗?否则

@numba.njit(fastmath=True, cache=True)
def fun(a, b=0):
    if b:
         return a['name']
    return a['age']

就可以。

英文:

Looks like it's not happy/does not support when the array index is unknown at compile time.

Do you need a solution beyond this example? Otherwise

@numba.njit(fastmath=True, cache=True)
def fun(a, b=0):
    if b:
         return a['name']
    return a['age']

should do

huangapple
  • 本文由 发表于 2023年5月22日 22:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定