pyspark.sql.functions.
to_date
Converts a Column into pyspark.sql.types.DateType using the optionally specified format. Specify formats according to datetime pattern. By default, it follows casting rules to pyspark.sql.types.DateType if the format is omitted. Equivalent to col.cast("date").
Column
pyspark.sql.types.DateType
col.cast("date")
New in version 2.2.0.
Changed in version 3.4.0: Supports Spark Connect.
input column of values to convert.
format to use to convert date values.
date value as pyspark.sql.types.DateType type.
Examples
>>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t).alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))]
>>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t']) >>> df.select(to_date(df.t, 'yyyy-MM-dd HH:mm:ss').alias('date')).collect() [Row(date=datetime.date(1997, 2, 28))]