Python 提高‘;PicklingError’;使用pyspark在特定类中应用函数时出错

Python 提高‘;PicklingError’;使用pyspark在特定类中应用函数时出错,python,apache-spark,pyspark,Python,Apache Spark,Pyspark,我正在尝试将spark中的pandas函数与ApplyPandas一起使用,当我在某个类中转换它时,它会引发如下错误:pickle.PicklingError:无法序列化对象:异常:似乎您正在尝试从广播变量、操作或转换引用SparkContext。SparkContext只能在驱动程序上使用,不能在工作程序上运行的代码中使用。有关更多信息,请参阅SPARK-5063 我的脚本在函数类型编码中运行良好: from scipy.stats import kendalltau import numpy

我正在尝试将spark中的pandas函数与ApplyPandas一起使用,当我在某个类中转换它时,它会引发如下错误:pickle.PicklingError:无法序列化对象:异常:似乎您正在尝试从广播变量、操作或转换引用SparkContext。SparkContext只能在驱动程序上使用,不能在工作程序上运行的代码中使用。有关更多信息,请参阅SPARK-5063

我的脚本在函数类型编码中运行良好:

from scipy.stats import kendalltau
import numpy as np
import pandas as pd

def kendall(dat, a, b):
        kentmp = []
        ken = [np.nan, np.nan]
        if type(a) is list:
            if dat.shape[0] > 3:
                for item in a:
                    kentmp.append(kendalltau(dat[item], dat[b])[0])
                tmp = pd.Series(kentmp, index=a).dropna()
                if tmp.shape[0] > 0:
                    cato = tmp.idxmax()
                    if (tmp < 0).any():
                        cato = tmp.abs().idxmax()
                    ken = [cato, tmp[cato]]
            index = ['category', 'corr']
        else:
            if dat.shape[0] >= 10:
                ken = [kendalltau(dat[a], dat[b])[0], dat.shape[0]]
            index = ['corr', 'N']
        return pd.Series(ken, index=index)

def kendall_process(pdf):
        result = pdf.groupby(['step_id','unit_id']).apply(kendall,'process','label')
        result = pd.DataFrame(result).reset_index()
        #result.columns = ['step_id','unit_id','corr','N']
        pdf['label'] = pdf.label.astype('int')
        result_ = pdf.groupby(['step_id','unit_id'])['label'].mean().reset_index()
        result = pd.merge(result,result_,on=['step_id','unit_id'],how='left')
        result.columns = ['step_id','unit_id','corr','N','ratio']
        return result
result = datInOut.groupBy('step_id','unit_id').applyInPandas(kendall_process, schema='step_id string,\
                                                                                        unit_id string,\
                                                                                         corr float,\
                                                                                       N long,\
                                                                                       ratio float')
                                                                                    
result.show(5)
+--------------+--------+-----------+----+-----+
|       step_id| unit_id|       corr|   N|ratio|
+--------------+--------+-----------+----+-----+
|10303_A2AOI300|A2AOI300|       null|null|  0.0|
|17613_A2AOI500|A2AOI500|-0.13477948|  14|  0.5|
|1B304_A2MAC100|A2MAC100|       null|null|  1.0|
|1A106_A2SPR100|A2SPR100|       null|null|  1.0|
|19103_A2AOI800|A2AOI800|       null|null|  0.5|
+--------------+--------+-----------+----+-----+
only showing top 5 rows
从scipy.stats导入kendalltau
将numpy作为np导入
作为pd进口熊猫
def kendall(数据、a、b):
肯特普=[]
肯=[np.nan,np.nan]
如果类型(a)为列表:
如果数据形状[0]>3:
对于a中的项目:
kentmp.append(kendalltau(日期[项目],日期[b])[0])
tmp=pd.Series(kentmp,index=a).dropna()
如果tmp.shape[0]>0:
cato=tmp.idxmax()
如果(tmp<0)。任何()
cato=tmp.abs().idxmax()
肯=[cato,tmp[cato]]
索引=['category','corr']
其他:
如果日期形状[0]>=10:
肯=[kendalltau(日期[a],日期[b])[0],日期形状[0]]
索引=['corr','N']
返回pd.系列(肯,索引=索引)
def kendall_过程(pdf):
结果=pdf.groupby(['step\u id','unit\u id'])。应用(kendall,'process','label'))
result=pd.DataFrame(result).reset_index()
#result.columns=['step\u id'、'unit\u id'、'corr'、'N']
pdf['label']=pdf.label.astype('int')
结果=pdf.groupby(['step\u id','unit\u id'])['label'].mean().reset\u index()
result=pd.merge(result,result,on=['step\u id','unit\u id'],how='left')
result.columns=['step\u id'、'unit\u id'、'corr'、'N'、'ratio']
返回结果
result=datInOut.groupBy('step\u id','unit\u id').applyinpandes(kendall\u进程,schema='step\u id string\
单元id字符串\
科尔浮动\
N long\
比率浮动')
结果显示(5)
+--------------+--------+-----------+----+-----+
|步骤id |单位id | corr | N |比率|
+--------------+--------+-----------+----+-----+
|10303_A2AOI300 | A2AOI300 |零|零| 0.0|
|17613| A2AOI500 | A2AOI500 |-0.13477948 | 14 | 0.5|
|1B304|A2MAC100 | A2MAC100 |空|空| 1.0|
|1A106_A2SPR100 | A2SPR100 |空|空| 1.0|
|19103_A2AOI800 | A2AOI800 |零|零| 0.5|
+--------------+--------+-----------+----+-----+
仅显示前5行
但当我将其转换为类类型编码时,它会引发PicklingError:

@staticmethod
def kendall(dat,a,b):
        kentmp=[]
        ken=[np.nan,np.nan]
        if type(a) is list:
            if dat.shape[0]>3:
                for item in a:
                    kentmp.append(kendalltau(dat[item],dat[b])[0])
                tmp=pd.Series(kentmp,index=a).dropna()
                if tmp.shape[0]>0:
                    cato=tmp.idxmax()
                    if (tmp<0).any():
                        cato=tmp.abs().idxmax()
                    ken=[cato,tmp[cato]]
            index=['category','corr']
        else:
            if dat.shape[0]>=10:
                ken=[kendalltau(dat[a],dat[b])[0],dat.shape[0]]
            index=['corr','N']
        return pd.Series(ken,index=index)
@staticmethod
def kendall_delay(pdf):
        result = pdf.groupby(['step_id','equip_id']).apply(QTWorker.kendall,'delay','label')
        result = pd.DataFrame(result).reset_index()
        pdf['label'] = pdf.label.astype('int')
        result_ = pdf.groupby(['step_id', 'equip_id'])['label'].mean().reset_index()
        result = pd.merge(result, result_, on=['step_id', 'equip_id'], how='left')
        result.columns = ['step_id', 'equip_id', 'corr', 'N', 'ratio']
        return result
ret = datQ.groupBy(self.step, self.equip).applyInPandas(self.kendall_delay, schema='step_id string,equip_id string,corr float,N long,ratio float')
@staticmethod
def kendall(数据、a、b):
肯特普=[]
肯=[np.nan,np.nan]
如果类型(a)为列表:
如果数据形状[0]>3:
对于a中的项目:
kentmp.append(kendalltau(日期[项目],日期[b])[0])
tmp=pd.Series(kentmp,index=a).dropna()
如果tmp.shape[0]>0:
cato=tmp.idxmax()
如果(tmp=10:
肯=[kendalltau(日期[a],日期[b])[0],日期形状[0]]
索引=['corr','N']
返回pd.系列(肯,索引=索引)
@静力学方法
def kendall_延迟(pdf):
result=pdf.groupby(['step_id','equipment_id'])。apply(QTWorker.kendall,'delay','label'))
result=pd.DataFrame(result).reset_index()
pdf['label']=pdf.label.astype('int')
结果=pdf.groupby(['step_id','equipment_id'])['label'].mean().reset_index()
result=pd.merge(result,result,on=['step\u id','equipment\u id',],how='left')
result.columns=['step_id'、'equipment_id'、'corr'、'N'、'ratio']
返回结果
ret=datQ.groupBy(self.step,self.equipment).applyPandas(self.kendall_delay,schema='step_id string,equipment_id string,corr float,N long,ratio float')

如你所见,我已经用staticmethod修饰了函数,但它仍然不起作用。我真的想知道如何修复它!

即使我不知道为什么,但我已经通过将kendall函数放在kendall_delay下解决了它。 我真的很想找出原因

@staticmethod
def kendall_process(pdf):
        def kendall(dat, a, b):
            kentmp = []
            ken = [np.nan, np.nan]
            if type(a) is list:
                if dat.shape[0] > 3:
                    for item in a:
                        kentmp.append(kendalltau(dat[item], dat[b])[0])
                    tmp = pd.Series(kentmp, index=a).dropna()
                    if tmp.shape[0] > 0:
                        cato = tmp.idxmax()
                        if (tmp < 0).any():
                            cato = tmp.abs().idxmax()
                        ken = [cato, tmp[cato]]
                index = ['category', 'corr']
            else:
                if dat.shape[0] >= 10:
                    ken = [kendalltau(dat[a], dat[b])[0], dat.shape[0]]
                index = ['corr', 'N']
            return pd.Series(ken, index=index)
        result = pdf.groupby(['step_id','equip_id']).apply(kendall,'process','label')
        result = pd.DataFrame(result).reset_index()
        pdf['label'] = pdf.label.astype('int')
        result_ = pdf.groupby(['step_id', 'equip_id'])['label'].mean().reset_index()
        result = pd.merge(result, result_, on=['step_id', 'equip_id'], how='left')
        result.columns = ['step_id', 'equip_id', 'corr', 'N', 'ratio']
        return result
@staticmethod
def kendall_过程(pdf):
def kendall(数据、a、b):
肯特普=[]
肯=[np.nan,np.nan]
如果类型(a)为列表:
如果数据形状[0]>3:
对于a中的项目:
kentmp.append(kendalltau(日期[项目],日期[b])[0])
tmp=pd.Series(kentmp,index=a).dropna()
如果tmp.shape[0]>0:
cato=tmp.idxmax()
如果(tmp<0)。任何()
cato=tmp.abs().idxmax()
肯=[cato,tmp[cato]]
索引=['category','corr']
其他:
如果日期形状[0]>=10:
肯=[kendalltau(日期[a],日期[b])[0],日期形状[0]]
索引=['corr','N']
返回pd.系列(肯,索引=索引)
结果=pdf.groupby(['step_id','equipment_id'])。应用(肯德尔,'process','label')
result=pd.DataFrame(result).reset_index()
pdf['label']=pdf.label.astype('int')
结果=pdf.groupby(['step_id','equipment_id'])['label'].mean().reset_index()
result=pd.merge(result,result,on=['step\u id','equipment\u id',],how='left')
result.columns=['step_id'、'equipment_id'、'corr'、'N'、'ratio']
返回结果