pyspark 返回pandas_udf spark内部的Pandas Series

6yoyoihd  于 4个月前  发布在  Spark
关注(0)|答案(2)|浏览(35)

在Apache Spark上,我有一个pandas_udf函数,它应该返回一个pd。系列如何将其存档?
我试探着:

@pandas_udf(ArrayType(LongType()), PandasUDFType.SCALAR_ITER) # Only works with spark 3.0
def udf(iterator):
  ...
  return pd.Series([1,2,3,4,5])

字符串
这给出了例外:

pyarrow.lib.ArrowNotImplementedError: NumPyConverter doesn't implement <list<item: int64>> conversion.

ws51t4hk

ws51t4hk1#

如果你想实现这样的目标:

+---+-----+---------------+
| id|value|     sample_col|
+---+-----+---------------+
|  1|    1|[1, 2, 3, 4, 5]|
|  2|    4|[1, 2, 3, 4, 5]|
|  3|    9|[1, 2, 3, 4, 5]|
|  4|   16|[1, 2, 3, 4, 5]|
|  5|   25|[1, 2, 3, 4, 5]|
+---+-----+---------------+

字符串
那么下面就可以了。

@pandas_udf(T.ArrayType(T.IntegerType())) 
def _udf(iterator:pd.Series) -> pd.Series:  
    result = pd.Series([[1,2,3,4,5] for _ in range(len(iterator))])
    return result
sdf.withColumn('sample_col',_udf((F.col('value')))).show()

的数据

sgtfey8w

sgtfey8w2#

这是我这边的一个错误。来自pandas udf的模式类型

相关问题