【问题标题】:pyspark pandas udf RuntimeError: Number of columns of the returned doesn't match specified schemapyspark pandas udf RuntimeError:返回的列数与指定的模式不匹配
【发布时间】:2020-12-03 18:21:53
【问题描述】:

我在下面定义了 pandas udf

schema2 = StructType([   StructField('sensorid', IntegerType(), True),
    StructField('confidence', DoubleType(), True)]) 

@pandas_udf(schema2,  PandasUDFType.GROUPED_MAP)   
def PreProcess(Indf):   
    confidence=1  
    sensor=Indf.iloc[0,0]   
    df = pd.DataFrame(columns=['sensorid','confidence'])  
    df['sensorid']=[sensor]   
    df['confidence']=[0]   
    return df

然后我将一个包含 3 列的 spark 数据框传递到该 udf 中

results.groupby("sensorid").apply(PreProcess)

results:
+--------+---------------+---------------+
|sensorid|sensortimestamp|calculatedvalue|
+--------+---------------+---------------+
|  397332|     1596518086|          -39.0|
|  397332|     1596525586|          -31.0|

但我不断收到此错误:

RuntimeError: Number of columns of the returned pandas.DataFrame doesn't match specified schema.Expected: 3 Actual: 4

我可以说出错误想说什么,但我不明白这个错误是如何弹出的。我以为我正在返回结构中指定的数据框的正确 2 列

【问题讨论】:

  • 另外,我在集群上的数据块笔记本中运行它。集群信息:42GB | 12核 | DBR6.4 |火花 2.4.5
  • @tryingtocode 你找到解决办法了吗

标签: python pandas apache-spark


【解决方案1】:

apply 已被弃用,它似乎期望返回相同的输入列,在这种情况下 3. 尝试将 applyInPandas 与预期的输出架构一起使用:

results.groupby("sensorid").applyInPandas(PreProcess, schema=schema2)

更新了最新版本的链接。 (Spark 的文档更改和链接已损坏)

在 3.0.0 版本中:applyapplyInPandas

【讨论】:

    猜你喜欢
    • 2019-02-26
    • 1970-01-01
    • 2020-09-03
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多