cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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​  !

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group