Lilly
Lilly

Reputation: 988

Pyspark column: Convert data in string format to timestamp format

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

Upvotes: 0

Views: 103

Answers (1)

Cena
Cena

Reputation: 3419

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|
+-----------------+

Upvotes: 1

Related Questions