cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Error of "name 'IntegerType' is not defined" in attempting to convert a DF column to IntegerType

BingQian
New Contributor II

initialDF .withColumn("OriginalCol", initialDF.OriginalCol.cast(IntegerType))

Or

initialDF .withColumn("OriginalCol", initialDF.OriginalCol.cast(IntegerType()))

However, always failed with this error :

NameError: name 'IntegerType' is not defined

Any help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

KristoRaun
New Contributor III

I'm assuming you are using Python.

In order to use the IntegerType, you first have to import it with the following statement:

from pyspark.sql.types import IntegerType

If you plan to have various conversions, it will make sense to import all types. This can be done as follows:

from pyspark.sql.types import *

For further reference, you can check the Spark documentation:

https://spark.apache.org/docs/latest/sql-reference.html

View solution in original post

2 REPLIES 2

KristoRaun
New Contributor III

I'm assuming you are using Python.

In order to use the IntegerType, you first have to import it with the following statement:

from pyspark.sql.types import IntegerType

If you plan to have various conversions, it will make sense to import all types. This can be done as follows:

from pyspark.sql.types import *

For further reference, you can check the Spark documentation:

https://spark.apache.org/docs/latest/sql-reference.html

BingQian
New Contributor II

Thank you @Kristo Raun​  !

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.