如何在Pyspark中将字符串更改为时间戳?

问题描述 投票:-2回答:1

我正在尝试使用以下数据集和api将字符串更改为pyspark中的时间戳(Spark版本= 2.3.0)

我一直在尝试从堆栈溢出的不同分辨率,但没有什么可以帮助改变time_stamp

df:
|Customer|Transaction_Timestamp|Transaction_Base_Point_Value|
+--------+---------------------+----------------------------+
|Cust1   |10/25/2017 1:47      |2000                        |

Attempt 1

df2 = df.select('Customer', 'Transaction_Timestamp','Transaction_Base_Point_Value', unix_timestamp('Transaction_Timestamp', "dd/MM/yy HH:mm") .cast(TimestampType()).alias("Timestamp")).show(1, False)

Attempt 2

df.withColumn('Time', to_timestamp("Transaction_Timestamp", "yyyy_MM_dd hh_mm_ss").cast("Timestamp"))

Attempt 3

change_type= df.withColumn('Timestamp', col='Transaction_Timestamp').cast('timestamp')

但是,架构生成以下输出

 |-- Timestamp: timestamp (nullable = true)

我需要得到如下输出,以便我可以在时间戳上执行其他操作

|Customer|Transaction_Timestamp|Transaction_Base_Point_Value|Timestamp|
+--------+---------------------+----------------------------+---------+
|   Cust1|      10/25/2017 1:47|                        2000|     10/25/2017 1:47|
python apache-spark pyspark apache-spark-sql
1个回答
-1
投票

使用来自to_timestamppyspark.sql.functions

.withColumn('Timestamp', to_timestamp('Transaction_Timestamp', 'MM/dd/yyyy hh:mm'))

如果没有1:47而且01:47,填充小时值也很好

© www.soinside.com 2019 - 2024. All rights reserved.