Python numba-打字机错误:无法确定的numba类型<;类别';内置函数或方法'&燃气轮机;

Python numba-打字机错误:无法确定的numba类型<;类别';内置函数或方法'&燃气轮机;,python,jit,numba,Python,Jit,Numba,我有一个简单的函数来排列扑克牌(牌是字符串) 我用rA,rB=rank(a),rank(b)来调用它,下面是我的实现。没有@jit(nopython=True)的情况下运行良好,但如果使用它,则会失败: File "...poker.py", line 190, in <module> rA,rB = rank(a),rank(b) File "C:\Continuum\anaconda3\lib\site-p

我有一个简单的函数来排列扑克牌(牌是字符串)

我用
rA,rB=rank(a),rank(b)
来调用它,下面是我的实现。没有@jit(nopython=True)的情况下运行良好,但如果使用它,则会失败:

   File "...poker.py", line 190, in <module>
        rA,rB = rank(a),rank(b)
    
      File "C:\Continuum\anaconda3\lib\site-packages\numba\dispatcher.py", line 344, in _compile_for_args
        reraise(type(e), e, None)
    
      File "C:\Continuum\anaconda3\lib\site-packages\numba\six.py", line 658, in reraise
        raise value.with_traceback(tb)

TypingError: cannot determine Numba type of <class 'builtin_function_or_method'>

Pandas和代码中的其他几个函数调用不能与
nopython=True
一起使用。nopython中可以与numba jit一起使用的可用库相当有限(几乎只限于numpy数组和某些python内置库)。您可以从中找到更多信息,不使用
@jit(nopython=True)
编译的代码在没有decorator的情况下可以更快

有趣的是,我发现我在这里研究的一个简单的示例在将Pandas列直接传递给我的函数to时,比使用numba并为numpy数组支付列的方法开销要快得多

但是,它将继续使用预期的参数来清除此警告

如果存在
@jit
装饰器有好处,那么为了将来证明,请提供关键字参数
forceobj=True
,以确保函数始终以对象模式编译


有没有办法绕过这个问题?(除了使用比熊猫更简单的东西)不幸的是,将你的系列转换成numpy阵列可能是你唯一的选择。我相信您可能还必须在没有内置排序方法的情况下对值进行排序。请记住,numba无法推断数据类型,因此您必须自己定义它们(例如,对于numpy数组
np.array(…,dtype=np.float64)
)。通常,只有使用nopython才能很好地工作的库是用CPython编写的
from numba import jit
from numba.types import string

@jit(nopython=True)
def rank(hand):
#    assert(len(hand) == 5)
    rank = "N/A"
    
    p = pd.Series([h[0] for h in hand]).value_counts()
    v = sorted(set(pd.Series([h[0] for h in hand]).values), reverse=True)
    s = sorted(hand, key=lambda k:k[0])    
    z = zip(s,s[1:])
    
    if all(x[0]==y[0]-1 for x,y in z):
        rank = "Straight "
    
    if len(set([h[1] for h in hand])) == 1:
        rank += "Flush "
    
    if "Straight Flush" in rank and sum([h[0] for h in hand]) == sum([10,11,12,13,14]):
        rank = "Royal Flush"
    
    elif p[p.idxmax()] == 4:
        rank = "4 Of A Kind : %d" % p.idxmax()
        
    elif p[p.idxmax()] == 3 and p[p.idxmin()] == 1:
        rank = "3 Of A Kind : %d" % p.idxmax()
        
    elif p[p.idxmax()] == 3 and p[p.idxmin()] == 2:
        rank = "Full House : %d,%d" % (p.idxmax(), p.idxmin())
        
    elif p[p.idxmax()] == 2:
        max2 = p.nlargest(2)
        
        if list(max2) == [2,2]:
            max2 = sorted(list(max2.keys()), reverse=True)
            rank = "2 Pairs : %d,%d" % (max2[0],max2[1])
        else:
            rank = "Pair : %d" % p.idxmax()
    
    else:
        rank = "High Card : %d" % v[0]
    

    return rank