Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

python - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...