Python 将字符串转换为日期列

Python 将字符串转换为日期列,python,Python,我想将字符串列转换为日期列,但结果是接收到的列具有空值 从pyspark.sql.functions导入expr,from_unixtime,dayofmonth,unix_timestamp,year,to_date,col dane3.withColumn('date',to_date(unix_时间戳(col('dateRep'),'%d.%m.%Y').cast(“时间戳”)).show() 您不需要像sql一样在d/m/y之前使用%。更多信息 df = spark.createDat

我想将字符串列转换为日期列,但结果是接收到的列具有空值

从pyspark.sql.functions导入expr,from_unixtime,dayofmonth,unix_timestamp,year,to_date,col
dane3.withColumn('date',to_date(unix_时间戳(col('dateRep'),'%d.%m.%Y').cast(“时间戳”)).show()

您不需要像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|
+---+----------+----------+