PySpark: TypeError: StructType 不能接受类型为 <type 'numpy.float64'> 的对象 0.10000000000000001 [英] PySpark: TypeError: StructType can not accept object 0.10000000000000001 in type <type 'numpy.float64'>

查看:26
本文介绍了PySpark: TypeError: StructType 不能接受类型为 <type 'numpy.float64'> 的对象 0.10000000000000001的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PySpark 和以下代码时:

when using PySpark with the following code:

from pyspark.sql.types import *
samples = np.array([0.1,0.2])
dfSchema = StructType([StructField("x", FloatType(), True)])
spark.createDataFrame(samples,dfSchema)

我明白了:

TypeError: StructType 不能接受类型为 0.10000000000000001 的对象输入'numpy.float64'>

TypeError: StructType can not accept object 0.10000000000000001 in type type 'numpy.float64'>

有什么想法吗?

推荐答案

NumPy 类型,包括 numpy.float64,不是 Spark SQL 类型的有效外部表示.此外,您使用的架构并不反映数据的形状.

NumPy types, including numpy.float64, are not a valid external representation for Spark SQL types. Furthermore schema you use doesn't reflect the shape of the data.

你应该使用标准的 Python 类型,并直接使用对应的 DataType:

You should use standard Python types, and corresponding DataType directly:

spark.createDataFrame(samples.tolist(), FloatType()).toDF("x")

这篇关于PySpark: TypeError: StructType 不能接受类型为 <type 'numpy.float64'> 的对象 0.10000000000000001的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆