PySpark DF字符串列到时间戳记为空

问题描述 投票:0回答:1

我正在尝试将字符串值转换为这种格式的时间戳:

+--------+
|tst     |
+--------+
|20191008|
|20191008|
|20191008|
+--------+

但是我遇到这个错误:

+----+
|tst2|
+----+
|null|
|null|
|null|
+----+

我已经尝试了所有这两种方法,但无法获得想要转换为'YYYY-MM-DD'时间戳的结果:

b = a.withColumn('tst2', to_date(a.tst , "yyyyMMDD"))

抱歉,我对此很陌生,所以我仍在学习基础知识。

pyspark databricks
1个回答
0
投票

尝试一下。只有MM应该大写。(只要tst是字符串)

from pyspark.sql import functions as F

a.withColumn("tst", F.to_date("tst", 'yyyyMMdd')).show()


+----------+
|       tst|
+----------+
|2019-10-08|
|2019-10-08|
|2019-10-08|
+----------+
© www.soinside.com 2019 - 2024. All rights reserved.