Skip to main content

to_binary

Converts the input col to a binary value based on the supplied format. The format can be a case-insensitive string literal of "hex", "utf-8", "utf8", or "base64". By default, the binary format for conversion is "hex" if format is omitted. The function returns NULL if at least one of the input parameters is NULL.

For the corresponding Databricks SQL function, see to_binary function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.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()