pyspark.sql.functions.try_to_timestamp#

pyspark.sql.functions.try_to_timestamp(col, format=None)[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 column name

column values to convert.

format: literal string, optional

format to use to convert timestamp values.

Examples

Example 1: Convert string to a timestamp

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t'])
>>> df.select(sf.try_to_timestamp(df.t).alias('dt')).show()
+-------------------+
|                 dt|
+-------------------+
|1997-02-28 10:30:00|
+-------------------+

Example 2: Convert string to a timestamp with a format

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([('1997-02-28 10:30:00',)], ['t'])
>>> df.select(sf.try_to_timestamp(df.t, sf.lit('yyyy-MM-dd HH:mm:ss')).alias('dt')).show()
+-------------------+
|                 dt|
+-------------------+
|1997-02-28 10:30:00|
+-------------------+

Example 3: Converion failure results in NULL when ANSI mode is on

>>> import pyspark.sql.functions as sf
>>> origin = spark.conf.get("spark.sql.ansi.enabled")
>>> spark.conf.set("spark.sql.ansi.enabled", "true")
>>> try:
...     df = spark.createDataFrame([('malformed',)], ['t'])
...     df.select(sf.try_to_timestamp(df.t)).show()
... finally:
...     spark.conf.set("spark.sql.ansi.enabled", origin)
+-------------------+
|try_to_timestamp(t)|
+-------------------+
|               NULL|
+-------------------+