pyspark.sql.functions.try_to_number#

pyspark.sql.functions.try_to_number(col, format)[source]#

Convert string ‘col’ to a number based on the string format format. Returns NULL if the string ‘col’ does not match the expected format. The format follows the same semantics as the to_number function.

New in version 3.5.0.

Parameters
colColumn or str

Input column or strings.

formatColumn or str, optional

format to use to convert number values.

Examples

Example 1: Convert a string to a number with a format specified

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([("$78.12",)], ["e"])
>>> df.select(sf.try_to_number(df.e, sf.lit("$99.99")).alias('r')).show()
+-----+
|    r|
+-----+
|78.12|
+-----+

Example 2: 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.range(1)
...     df.select(sf.try_to_number(sf.lit("77"), sf.lit("$99.99")).alias('r')).show()
... finally:
...     spark.conf.set("spark.sql.ansi.enabled", origin)
+----+
|   r|
+----+
|NULL|
+----+