Warm tip: This article is reproduced from serverfault.com, please click

pyspark convert millisecond timestamp to timestamp

发布于 2020-12-08 17:52:17

I am new in spark and i would like to retrieve timestamps in my DF.

checkpoint actual values

1594976390070

and i want :

checkpoint values without ms

1594976390070 / 1000

Actually i am using this piece of code to cast as timestamp:

# Casting dates as Timestamp
for d in dateFields:
    df= df.withColumn(d,checkpoint.cast(TimestampType()))

I wonder how to convert it into a simple timestamp.

Questioner
Jonito
Viewed
0
mck 2020-12-09 02:32:42

Divide your column by 1000 and use F.from_unixtime to convert to timestamp type:

import pyspark.sql.functions as F

for d in dateFields:
    df = df.withColumn(d,
        (checkpoint / F.lit(1000.)).cast('timestamp')
    )