首页 > 解决方案 > 将字符串转换为日期列

问题描述

我想将 String 列转换为 Date 列,但结果我收到了带有空值的列。

from pyspark.sql.functions import expr, from_unixtime, dayofmonth, unix_timestamp, year, to_date, col

dane3.withColumn('date', to_date(unix_timestamp(col('dateRep'),'%d.%m.%Y').cast("timestamp"))).show()

在此处输入图像描述

标签: python

解决方案


你不需要像sql这样的%之前。文档中的d/m/y更多信息

df = spark.createDataFrame(
    [
        (1, '27.08.2020'), 
        (2, '27.08.2019'),
    ],
    ['id', 'txt']
)

df = df.withColumn('formatted', 
                   to_date(unix_timestamp(col('txt'), 'dd.MM.yyyy').cast("timestamp")))
df.show()

+---+----------+----------+
| id|       txt| formatted|
+---+----------+----------+
|  1|27.08.2020|2019-12-29|
|  2|27.08.2019|2018-12-30|
+---+----------+----------+

推荐阅读