numpy 引发错误:TypeError:无法推断类型的架构:<class 'numpy.float64'>

Posted

技术标签:

【中文标题】numpy 引发错误:TypeError:无法推断类型的架构:<class \'numpy.float64\'>【英文标题】: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: &lt;class 'numpy.float64'&gt; 错误。 numpy.int64 数组也会发生同样的事情。

例子:

df = spark.createDataFrame(numpy.arange(10.))

TypeError:无法推断类型的架构:

【问题讨论】:

【参考方案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.)])

【讨论】:

以上是关于numpy 引发错误:TypeError:无法推断类型的架构:<class 'numpy.float64'>的主要内容,如果未能解决你的问题,请参考以下文章