我在下面定义了 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 数据框传递到该 udfresults.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 列
最佳答案
apply
已弃用,似乎期望返回相同的输入列,在本例中为 3。尝试使用 applyInPandas
使用预期的输出模式:
results.groupby("sensorid").applyInPandas(PreProcess, schema=schema2)
更新了最新版本的链接。 (Spark 的文档更改和链接已损坏)在 3.0.0 版中:
apply
applyInPandas
关于python - pyspark Pandas udf 运行时错误 : Number of columns of the returned doesn't match specified schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63403001/