在 PySpark 中将整数列转换为日期

Dim*_*mpu 4 apache-spark-sql pyspark

birth_date我有一个以这种格式调用的整数列:20141130

我想将其转换为2014-11-30PySpark 中的内容。

这会错误地转换日期:

.withColumn("birth_date", F.to_date(F.from_unixtime(F.col("birth_date"))))
Run Code Online (Sandbox Code Playgroud)

这给出了一个错误: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')))
Run Code Online (Sandbox Code Playgroud)

将其转换为我想要的日期的最佳方法是什么?

Sur*_*ali 6

在将列传递给函数之前,将birth_date列从转换Integer为:Stringto_date

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|
+----------+
Run Code Online (Sandbox Code Playgroud)