gpt4 book ai didi

python - 在 Pyspark 中将纪元时间转换为时间戳

转载 作者:行者123 更新时间:2023-12-05 06:19:58 25 4
gpt4 key购买 nike

我有一个 df,其中有一列有纪元时间。纪元时间列的变量类型为字符串。我希望它转换成时间戳。我正在使用以下命令

from pyspark.sql.functions import from_utc_timestamp
df = df.withColumn('start_time',from_unixtime(df.recognition_start_time,'UTC'))
df.select('recognition_start_time').show(10,False)

但命令结果为 null。我犯了什么错误?

Epoch Time : 1583471040000
Output expected :2020-03-06 05:04:00

最佳答案

from_unixtime 中,我们需要指定预期的输出格式 (yyyy-MM-dd hh:mm:ss) 并使用 < strong>from_utc_timestamp 我们可以将输出从 utc 转换为给定时区。

示例:

spark.sql("""select from_utc_timestamp(from_unixtime("1583471040000"/1000,"yyyy-MM-dd hh:mm:ss"),"America/Chicago")""").show(false)
+---------------------+
|_c0 |
+---------------------+
|2020-03-05 05:04:00.0|
+---------------------+

关于python - 在 Pyspark 中将纪元时间转换为时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60652032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com