gpt4 book ai didi

pyspark - unix_timestamp 是否截断或舍入毫秒?

转载 作者:行者123 更新时间:2023-12-01 23:56:24 27 4
gpt4 key购买 nike

来自reference :

Convert time string with given pattern (‘yyyy-MM-dd HH:mm:ss’, by default) to Unix time stamp (in seconds), using the default timezone and the default locale, return null if fail.

我发现这会减少 DataFrame 时间戳列的毫秒数。我只是想知道它是否只是简单地截断或将时间戳四舍五入到最接近的秒。

最佳答案

没有文档备份,但在 @spark 2.2.0 中,它是截断,这是一个演示:

from pyspark.sql import Row
import pyspark.sql.functions as F
r = Row('datetime')
lst = [r('2017-10-29 10:20:30.102'), r('2017-10-29 10:20:30.999')]

df = spark.createDataFrame(lst)

(df.withColumn('trunc_datetime', F.unix_timestamp(F.col('datetime')))
.withColumn('seconds', F.from_unixtime(F.col('trunc_datetime'), 'ss'))
.show(2, False))

+-----------------------+--------------+-------+
|datetime |trunc_datetime|seconds|
+-----------------------+--------------+-------+
|2017-10-29 10:20:30.102|1509286830 |30 |
|2017-10-29 10:20:30.999|1509286830 |30 |
+-----------------------+--------------+-------+

关于pyspark - unix_timestamp 是否截断或舍入毫秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47007740/

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