pyspark:ValueError:推断后无法确定某些类型

时间:2016-11-09 23:11:39

标签: python python-2.7 pandas pyspark spark-dataframe

我有一个pandas数据框my_dfmy_df.dtypes给了我们:

ts              int64
fieldA         object
fieldB         object
fieldC         object
fieldD         object
fieldE         object
dtype: object

然后我尝试将pandas数据框my_df转换为火花数据框,方法如下:

spark_my_df = sc.createDataFrame(my_df)

但是,我收到了以下错误:

ValueErrorTraceback (most recent call last)
<ipython-input-29-d4c9bb41bb1e> in <module>()
----> 1 spark_my_df = sc.createDataFrame(my_df)
      2 spark_my_df.take(20)

/usr/local/spark-latest/python/pyspark/sql/session.py in createDataFrame(self, data, schema, samplingRatio)
    520             rdd, schema = self._createFromRDD(data.map(prepare), schema, samplingRatio)
    521         else:
--> 522             rdd, schema = self._createFromLocal(map(prepare, data), schema)
    523         jrdd = self._jvm.SerDeUtil.toJavaArray(rdd._to_java_object_rdd())
    524         jdf = self._jsparkSession.applySchemaToPythonRDD(jrdd.rdd(), schema.json())

/usr/local/spark-latest/python/pyspark/sql/session.py in _createFromLocal(self, data, schema)
    384 
    385         if schema is None or isinstance(schema, (list, tuple)):
--> 386             struct = self._inferSchemaFromList(data)
    387             if isinstance(schema, (list, tuple)):
    388                 for i, name in enumerate(schema):

/usr/local/spark-latest/python/pyspark/sql/session.py in _inferSchemaFromList(self, data)
    318         schema = reduce(_merge_type, map(_infer_schema, data))
    319         if _has_nulltype(schema):
--> 320             raise ValueError("Some of types cannot be determined after inferring")
    321         return schema
    322 

ValueError: Some of types cannot be determined after inferring

有谁知道上述错误是什么意思?谢谢!

5 个答案:

答案 0 :(得分:17)

为了推断字段类型,PySpark会查看每个字段中的非none记录。如果某个字段只有None记录,则PySpark无法推断该类型并会引发该错误。

手动定义架构将解决问题

>>> from pyspark.sql.types import StructType, StructField, StringType
>>> schema = StructType([StructField("foo", StringType(), True)])
>>> df = spark.createDataFrame([[None]], schema=schema)
>>> df.show()
+----+
|foo |
+----+
|null|
+----+

答案 1 :(得分:3)

And to fix this problem, you could provide your own defined schema.

For example:

To reproduce the error:

>>> df = spark.createDataFrame([[None, None]], ["name", "score"])

To fix the error:

>>> from pyspark.sql.types import StructType, StructField, StringType, DoubleType
>>> schema = StructType([StructField("name", StringType(), True), StructField("score", DoubleType(), True)])
>>> df = spark.createDataFrame([[None, None]], schema=schema)
>>> df.show()
+----+-----+
|name|score|
+----+-----+
|null| null|
+----+-----+

答案 2 :(得分:0)

如果您使用的是RDD[Row].toDF()猴子补丁方法,则可以在推断类型时提高采样率以检查100多个记录:

my_df = my_rdd.toDF(sampleRatio=0.1)
my_df.show()

假设RDD中所有字段中都有非空行,那么当您将sampleRatio增加到1.0时,更有可能找到它们。

答案 3 :(得分:0)

这可能是因为这些列都具有空值。您应先删除这些列,然后再将其转换为spark数据框

答案 4 :(得分:0)

我也遇到过同样的问题,如果您不需要为空的列,则可以在导入到spark之前将它们从pandas数据框中删除:

my_df = my_df.dropna(axis='columns', how='all') # Drops columns with all NA values
spark_my_df = sc.createDataFrame(my_df)