pyspark.sql.functions.try_to_timestamp

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.

Parameters
colColumn or str

column values to convert.

format: str, optional

format to use to convert timestamp values.

Examples

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