简体   繁体   中英

Pyspark date format

I need to convert a string into a date format, for this the string format I have is something like this:

2021-01-07 11:17:49.385820+00:00

and what I am doing is the following:

    format = "yyyy-MM-dd HH:mm:ss.SSSSSS+ZZ:ZZ"
    level_1_data_df = level_1_data_value_df\
        .select(
        "level_1_data.*"
        ).withColumn("time2", to_timestamp(col("time"), format))

but the result of the new field is null, any ideas?

You can simply do

.withColumn("time2", to_timestamp(col("time")))

or, equivalently,

.withColumn("time2", col("time").cast("timestamp"))

because your timestamp has a standard format. No need for specifying its format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM