gpt4 book ai didi

apache-spark - pyspark to_timestamp 不包括毫秒

转载 作者:行者123 更新时间:2023-12-03 23:14:08 28 4
gpt4 key购买 nike

我正在尝试格式化我的时间戳列以包含毫秒但没有成功。我怎样才能把我的时间格式化成这样 - 2019-01-04 11:09:21.152 ?

我查看了文档并遵循了 SimpleDataTimeFormat ,pyspark 文档说 to_timestamp 正在使用它功能。

这是我的数据框。

+--------------------------+
|updated_date |
+--------------------------+
|2019-01-04 11:09:21.152815|
+--------------------------+

我使用毫秒格式没有任何成功,如下所示
>>> df.select('updated_date').withColumn("updated_date_col2", 
to_timestamp("updated_date", "YYYY-MM-dd HH:mm:ss:SSS")).show(1,False)
+--------------------------+-------------------+
|updated_date |updated_date_col2 |
+--------------------------+-------------------+
|2019-01-04 11:09:21.152815|2019-01-04 11:09:21|
+--------------------------+-------------------+

我期待 updated_date_col2格式化为 2019-01-04 11:09:21.152

最佳答案

我认为您可以使用 UDF 和 Python 的标准日期时间模块,如下所示。

import datetime
from pyspark.sql.functions import udf
from pyspark.sql.types import TimestampType

def _to_timestamp(s):
return datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f')

udf_to_timestamp = udf(_to_timestamp, TimestampType())

df.select('updated_date').withColumn("updated_date_col2", udf_to_timestamp("updated_date")).show(1,False)

关于apache-spark - pyspark to_timestamp 不包括毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54044012/

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