Pyspark: Extract date from Datetime value

Pyspark has a to_date function to extract the date from a timestamp. In your example you could create a new column with just the date by doing the following:

from pyspark.sql.functions import col, to_date

df = df.withColumn('date_only', to_date(col('date_time')))

If the column you are trying to convert is a string you can set the format parameter of to_date specifying the datetime format of the string.

You can read more about to_date in the documentation here.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)