0

I am trying to convert one pyspark column in string format to timestamp format

I tried as below but I get null values for that column.

Thanks

Example data:   06/19/17 00:00:00

df = df.withColumn("date", to_timestamp(col("date"), "MM/dd/yyy HHmm"))

When I tried casting as below

df = df.withColumn("date",(col("invoice_date").cast(TimestampType())))

output: 2017-06-19T00:00:00.000+0000

Expected output:

 06/19/17 00:00:00

1 Answer 1

1

You can do:

from pyspark.sql import functions as F

df.withColumn("date", F.from_unixtime(F.unix_timestamp("date", \
    'MM/dd/yy HH:mm:ss'),'MM-dd-yy HH:mm:ss')).show()

+-----------------+
|             date|
+-----------------+
|06-19-17 00:00:00|
+-----------------+
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.