Pandas 应用于自定义函数导致分段错误

Pandas apply on custom function results in segmetnation fault

我正在使用 apply 方法在数据框上应用自定义方法。当传递超过 2 行(元组)的数据帧时,它会导致内核在 jupyter notebook 中终止(死亡)。当 运行 在终端上相同时,会导致 分段错误

该方法适用于单行或 2 行,但不能超过 2 行。下面的两个调用都适用于自定义函数 myTrial.

myTrial(pd.ix[3,:])
newPD2 = pd.head(2).apply(myTrial, axis=1)

但这会导致以下错误。

newPD2 = pd.head(3).apply(myTrial, axis=1)
The kernel appears to have died. It will restart automatically.

方法 myTrial 使用 BioPython 中的对齐函数 pairwise2.align.globalmx 和其他内置的 python 函数。我提供以下功能:

我有一个包含 10,000 行和 8 列的数据框。我正在起诉具有 256 GB RAM 的服务器。

函数如下

from Bio import pairwise2
def myTrial(pdf):
    source = pdf['source']
    targ = pdf['target']

    if source == targ:
        pdf['sourceAlign'] = source
        pdf['targetAlign'] = source
        pdf['joint'] = source

        return pdf

    alignments = pairwise2.align.globalmx(source, targ,1,-0.5)
    summaDict = dict()
    for item in alignments:
        lenList = list()
        i = 0
        while i < len(item[0]):
            con = 0
            while item[0][i] == item[1][i]:
                con += 1
                i += 1

            if con == 0:
                i += 1
            else:
                lenList.append((con,item[0][i-con:i],item))
                con =0

        summa = 0
        for thing in lenList:
            summa += (thing[0]*thing[0])
        try:
            summaDict[summa].append(lenList)
        except:
            summaDict[summa] = list()
            summaDict[summa].append(lenList)
    stuff = sorted(summaDict.keys(),reverse=True)[0]

    if len(summaDict[stuff]) > 1:
        print(source,targ,summaDict[stuff])

    words = summaDict[stuff][0][0][2]

    jointWord = ''
    for inda in range(len(words[0])):
        if words[0][inda] == words[1][inda]:
            jointWord += words[0][inda]
        else:
            if words[0][inda] != '-':
                jointWord += 'DEL('+words[0][inda]+')'
            if words[1][inda] != '-':
                jointWord += 'INS('+words[1][inda]+')'

    pdf['sourceAlign'] = words[0]
    pdf['targetAlign'] = words[1]
    pdf['joint'] = jointWord

    return pdf

dataframe如下

type |  source |    props | target |    subtype |   p0 |    p1 |    p2 |    p3 |    p4
        0 | ADJ |   najprzytulniejszy | [NEUT, INS, SG] |   najprzytulniejszym |    NaN |   NEUT |  INS |   SG |    None |  None
        1 | ADJ |   sadystyczny |   [MASC, DAT, SG] |   sadystycznemu | NaN |   MASC |  DAT |   SG |    None |  None
        2 | V | wyrzucić |  [FUT, 2, SG] |  wyrzucisz | NaN |   FUT |   2 | SG |    None |  None
        3 | N | świat | [ACC, SG] | świat | NaN |   ACC |   SG |    None |  None |  None
        4 | N | Marsjanin | [INS, PL] | Marsjanami |    NaN |   INS |   PL |    None |  None |  None

控制台输出也没有帮助:

[I 19:16:45.709 NotebookApp] Kernel restarted: 604e9df5-6630-4a12-9c13-e9d7a4835da2
[I 19:17:00.710 NotebookApp] KernelRestarter: restarting kernel (1/5)
WARNING:root:kernel 604e9df5-6630-4a12-9c13-e9d7a4835da2 restarted

可能的原因是什么?

Bio.pairwise2.globalmx 函数导致段错误,这是 Pandas 无法控制的。有关如何修复底层段错误的解决方案,请参阅