Skip to main content

try_to_binary

This is a special version of to_binary that performs the same operation, but returns a NULL value instead of raising an error if the conversion cannot be performed.

For the corresponding Databricks SQL function, see try_to_binary function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.try_to_binary(col=<col>, format=<format>)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or str

Input column or strings.

format

pyspark.sql.Column or str, optional

format to use to convert binary values.

Examples

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([("abc",)], ["e"])
df.select(dbf.try_to_binary(df.e, dbf.lit("utf-8")).alias('r')).collect()
Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([("414243",)], ["e"])
df.select(dbf.try_to_binary(df.e).alias('r')).collect()