0

I'm trying to convert a string column to Timestamp column which is in the format:

c1 c2
2019-12-10 10:07:54.000 2019-12-13 10:07:54.000
2020-06-08 15:14:49.000 2020-06-18 10:07:54.000
from pyspark.sql.functions import col, udf, to_timestamp

joined_df.select(to_timestamp(joined_df.c1, '%Y-%m-%d %H:%M:%S.%SSSS').alias('dt')).collect()
joined_df.select(to_timestamp(joined_df.c2, '%Y-%m-%d %H:%M:%S.%SSSS').alias('dt')).collect()

When the dates are changed, I want a new column Date difference by subtracting c2-c1

In python I'm doing it:

df['c1']        = df['c1'].fillna('0000-01-01').apply(lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f'))

df['c2'] = df['c2'].fillna('0000-01-01').apply(lambda x:  datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f'))

df['days']     = (df['c2'] - df['c1']).apply(lambda x: x.days)

Can anyone help how to convert to pyspark?

mck
  • 40,932
  • 13
  • 35
  • 50

1 Answers1

0

If you want to get the date difference, you can use datediff:

import pyspark.sql.functions as F

df = df.withColumn('c1', F.col('c1').cast('timestamp')).withColumn('c2', F.col('c2').cast('timestamp'))
result = df.withColumn('days', F.datediff(F.col('c2'), F.col('c1')))
result.show(truncate=False)
+-----------------------+-----------------------+----+
|c1                     |c2                     |days|
+-----------------------+-----------------------+----+
|2019-12-10 10:07:54.000|2019-12-13 10:07:54.000|3   |
|2020-06-08 15:14:49.000|2020-06-18 10:07:54.000|10  |
+-----------------------+-----------------------+----+
mck
  • 40,932
  • 13
  • 35
  • 50