4
에 int로 캐스팅 할 때

나는 pyspark의 dataframe에 IntType에 StringType 캐스트 동안 오류가 있어요 :예기치 않은 유형 : <클래스 'pyspark.sql.types.DataTypeSingleton'>를 ApacheSpark Dataframe

joint = aggregates.join(df_data_3,aggregates.year==df_data_3.year) 
joint2 = joint.filter(joint.CountyCode==999).filter(joint.CropName=='WOOL')\ 
    .select(aggregates.year,'Production')\ 
    .withColumn("ProductionTmp", df_data_3.Production.cast(IntegerType))\ 
    .drop("Production")\ 
    .withColumnRenamed("ProductionTmp", "Production") 

나는 점점되지 해요 :

TypeErrorTraceback (most recent call last) in() 1 joint = aggregates.join(df_data_3,aggregates.year==df_data_3.year) ----> 2 joint2 = joint.filter(joint.CountyCode==999).filter(joint.CropName=='WOOL')
.select(aggregates.year,'Production') .withColumn("ProductionTmp", df_data_3.Production.cast(IntegerType)) .drop("Production")
.withColumnRenamed("ProductionTmp", "Production")

/usr/local/src/spark20master/spark/python/pyspark/sql/column.py in cast(self, dataType) 335 jc = self._jc.cast(jdt) 336 else: --> 337 raise TypeError("unexpected type: %s" % type(dataType)) 338 return Column(jc) 339

TypeError: unexpected type:

답변

4

PySpark SQL 데이터 유형이 더 이상 싱글 (이 1.3 이전의 사건이었다).

col("foo").cast(IntegerType) 
TypeError 
    ... 
TypeError: unexpected type: <class 'type'> 

cast 방법도 문자열 설명과 함께 사용할 수 있습니다 :

col("foo").cast("integer") 
Column<b'CAST(foo AS INT)'> 
대조적으로

from pyspark.sql.types import IntegerType 
from pyspark.sql.functions import col 

col("foo").cast(IntegerType()) 
Column<b'CAST(foo AS INT)'> 

: 당신은 인스턴스를 생성해야