Issue
I have an Integer column called birth_date in this format: 20141130
I want to convert that to 2014-11-30 in PySpark.
This converts the date incorrectly:
.withColumn("birth_date", F.to_date(F.from_unixtime(F.col("birth_date"))))
This gives an error: argument 1 requires (string or date or timestamp) type, however, 'birth_date' is of int type
.withColumn('birth_date', F.to_date(F.unix_timestamp(F.col('birth_date'), 'yyyyMMdd').cast('timestamp')))
What is the best way to convert it to the date I want?
Solution
Convert the birth_date column from Integer to String before you pass it to the to_date function:
from pyspark.sql import functions as F
df.withColumn("birth_date", F.to_date(F.col("birth_date").cast("string"), \
'yyyyMMdd')).show()
+----------+
|birth_date|
+----------+
|2014-11-30|
+----------+
Answered By - Cena Answer Checked By - Willingham (sql.easybugfix Volunteer)
Post a Comment
Post a Comment