pyspark.sql.functions.try_to_timestamp(col: ColumnOrName, format: Optional[ColumnOrName] = None) → pyspark.sql.column.Column[source]

Parses the col with the format to a timestamp. The function always returns null on an invalid input with/without ANSI SQL mode enabled. The result data type is consistent with the value of configuration spark.sql.timestampType.

New in version 3.5.0.

colColumn or str

column values to convert.

format: str, optional

format to use to convert timestamp values.


>>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t'])
[Row(dt=datetime.datetime(1997, 2, 28, 10, 30))]
>>>, lit('yyyy-MM-dd HH:mm:ss')).alias('dt')).collect()
[Row(dt=datetime.datetime(1997, 2, 28, 10, 30))]