【问题标题】: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:无法推断类型的架构:

【问题讨论】:

    标签: 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.)])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-17
        • 1970-01-01
        • 2015-12-20
        相关资源
        最近更新 更多