2016-06-24 2 views
2

나는 단지 4 열의 float64 인 팬더 데이터 프레임이 pdf입니다.유형 팬더 데이터 프레임을 데이터 프레임을 스파크로 변환하는 오류

pdf[:5] 

     x1   x2  x3   y 
0 9.082060 12.837502 6.484107 10.985202 
1 9.715981 14.870818 8.026042 12.815644 
2 11.303901 21.286343 7.787188 15.786915 
3 9.910293 20.533151 6.991775 14.775010 
4 12.394907 15.401446 7.101058 13.213897 

그리고 dtypes : 여기 처음 다섯 줄 수 있습니다

pdf.dtypes 

x1 float64 
x2 float64 
x3 float64 
y  float64 
dtype: object 

하지만 스파크 dataframe에이 변환하려고하면

sdf = sqlContext.createDataFrame(pdf) 

TypeErrorTraceback (most recent call last) 
<ipython-input-54-a40cb79104b5> in <module>() 
     5      ]) 
     6 
----> 7 sdf = sqlContext.createDataFrame(pdf) 

/usr/lib/spark/python/pyspark/sql/context.py in createDataFrame(self, data, schema, samplingRatio) 
    423    rdd, schema = self._createFromRDD(data, schema, samplingRatio) 
    424   else: 
--> 425    rdd, schema = self._createFromLocal(data, schema) 
    426   jrdd = self._jvm.SerDeUtil.toJavaArray(rdd._to_java_object_rdd()) 
    427   jdf = self._ssql_ctx.applySchemaToPythonRDD(jrdd.rdd(), schema.json()) 

/usr/lib/spark/python/pyspark/sql/context.py in _createFromLocal(self, data, schema) 
    339 
    340   if schema is None or isinstance(schema, (list, tuple)): 
--> 341    struct = self._inferSchemaFromList(data) 
    342    if isinstance(schema, (list, tuple)): 
    343     for i, name in enumerate(schema): 

/usr/lib/spark/python/pyspark/sql/context.py in _inferSchemaFromList(self, data) 
    239    warnings.warn("inferring schema from dict is deprecated," 
    240       "please use pyspark.sql.Row instead") 
--> 241   schema = reduce(_merge_type, map(_infer_schema, data)) 
    242   if _has_nulltype(schema): 
    243    raise ValueError("Some of types cannot be determined after inferring") 

/usr/lib/spark/python/pyspark/sql/types.py in _infer_schema(row) 
    829 
    830  else: 
--> 831   raise TypeError("Can not infer schema for type: %s" % type(row)) 
    832 
    833  fields = [StructField(k, _infer_type(v), True) for k, v in items] 

TypeError: Can not infer schema for type: <type 'str'> 

나는를 지정하려고하면 스키마 :

schema = StructType([StructField('y', DoubleType()), 
        StructField('x1', DoubleType()), 
        StructField('x2', DoubleType()), 
        StructField('x3', DoubleType()) 
        ]) 
sdf = sqlContext.createDataFrame(pdf, schema) 

그런 다음 약간 다른 오류가 발생합니다.

TypeErrorTraceback (most recent call last) 
<ipython-input-55-a7d2b6d09ed3> in <module>() 
     5      ]) 
     6 
----> 7 sdf = sqlContext.createDataFrame(pdf, schema) 

/usr/lib/spark/python/pyspark/sql/context.py in createDataFrame(self, data, schema, samplingRatio) 
    423    rdd, schema = self._createFromRDD(data, schema, samplingRatio) 
    424   else: 
--> 425    rdd, schema = self._createFromLocal(data, schema) 
    426   jrdd = self._jvm.SerDeUtil.toJavaArray(rdd._to_java_object_rdd()) 
    427   jdf = self._ssql_ctx.applySchemaToPythonRDD(jrdd.rdd(), schema.json()) 

/usr/lib/spark/python/pyspark/sql/context.py in _createFromLocal(self, data, schema) 
    348   elif isinstance(schema, StructType): 
    349    for row in data: 
--> 350     _verify_type(row, schema) 
    351 
    352   else: 

/usr/lib/spark/python/pyspark/sql/types.py in _verify_type(obj, dataType) 
    1132  if _type is StructType: 
    1133   if not isinstance(obj, (tuple, list)): 
-> 1134    raise TypeError("StructType can not accept object %r in type %s" % (obj, type(obj))) 
    1135  else: 
    1136   # subclass of them can not be fromInternald in JVM 

TypeError: StructType can not accept object 'x1' in type <type 'str'> 

나는 뭔가 분명한 것을 놓치고 있습니까? 누구든지 팬더 데이터 프레임에서 스파크 데이터 프레임을 성공적으로 구축 했습니까? 이것은 Python 2.7, Spark v1.6.1 및 Pandas v0.18.1에 있습니다.

+0

흠 ... 나는 열 머리글을 가져 와서 데이터로 취급하려고하고 있다고 생각합니다. 머리말을 꺼내보십시오. 나는이 섹션의 마지막 예제 (http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.SQLContext.createDataFrame)에서 가정을 맺고있다. –

+0

확실히 헤더와 관련이있는 것 같습니다. 정수로 변경하면 문자열에 대한 경고에서 'TypeError : 유형 : '에 대한 스키마를 추론 할 수 없습니다. 하지만 팬더 데이터 프레임에 헤더가 전혀 없을 수 있다고 생각하지 않습니까? – Jeff

+0

'python 2.7.10','spark 1.6.0' 및'pandas 0.16.2'에서 잘 작동합니다. – shivsn

답변

0

성공적으로 이것을 재현했으며, ipython 노트북을 닫았다가 다시 열어 놓은 것처럼 보입니다. Python 2.7 이외의 새로운 클러스터를 만들 때, pip와 numpy가 설치되고 (bootstrap에 기본 설정) pip.main()을 사용하여 Pandas 0.18.1을 설치 한 다음 createDataFrame()을 사용하여 Spark 데이터 프레임으로 변환하려고 시도합니다. 위의 오류와 함께 실패합니다. 그러나 노트북을 닫고 정지 한 다음 다시 시작하면 정상적으로 작동합니다.

관련 문제