【问题标题】:numpy to spark error: TypeError: Can not infer schema for type: <class 'numpy.float64'>numpy 引发错误:TypeError:无法推断类型的架构:<class 'numpy.float64'>
【发布时间】:2022-01-22 06:45:38
【问题描述】:
在尝试将 numpy 数组转换为 Spark DataFrame 时,我收到 Can not infer schema for type: <class 'numpy.float64'> 错误。
numpy.int64 数组也会发生同样的事情。
例子:
df = spark.createDataFrame(numpy.arange(10.))
TypeError:无法推断类型的架构:
【问题讨论】:
标签:
pandas
numpy
apache-spark
pyspark
【解决方案1】:
快速转换为 pandas DataFrame 效果很好:
import pandas
import numpy
df = spark.createDataFrame(pandas.DataFrame(numpy.arange(10.)))
【解决方案2】:
或者不使用熊猫:
df = spark.createDataFrame([(float(i),) for i in numpy.arange(10.)])