如何在pyspark中将字符串中的AM / PM转换为DateTime?

时间:2018-08-03 22:03:29

标签: datetime pyspark

我的datetime的格式如下:

           visit_dts |web_datetime|
+--------------------+------------+
| 5/1/2018 3:48:14 PM|        null|

基于提供的答案here,我正在使用以下查询将字符串转换为datetime格式:

web1 = web1.withColumn("web_datetime", from_unixtime(unix_timestamp(col("visit_dts"), "%mm/%dd/%YY %I:%M:%S %p")))

但是它不起作用。任何线索都很好。

2 个答案:

答案 0 :(得分:0)

您可以按照下面的步骤操作来达到目的

from pyspark.sql import Row

df = sc.parallelize([Row(visit_dts='5/1/2018 3:48:14 PM')]).toDF()

import pyspark.sql.functions as f

web = df.withColumn("web_datetime", f.from_unixtime(f.unix_timestamp("visit_dts",'MM/dd/yyyy hh:mm:ss aa'),'MM/dd/yyyy HH:mm:ss'))

这应该给你

web.show()

+-------------------+-------------------+
|          visit_dts|       web_datetime|
+-------------------+-------------------+
|5/1/2018 3:48:14 PM|05/01/2018 15:48:14|
+-------------------+-------------------+

答案 1 :(得分:0)

这对我来说很完美

overlayWindow.windowLevel = UIWindow.Level.alert
overlayWindow.rootViewController = UIViewController()//your controller or navController
overlayWindow.makeKeyAndVisible()

这给了

from pyspark.sql.functions import to_timestamp
df=spark.read.csv(fp,header=True)
df=df.withColumn('time',to_timestamp("Date","MM/dd/yyyy hh:mm:ss a"))
df.select("Case Number",'time','Date').show(5,False)