pyspark.sql.functions.to_binary#

pyspark.sql.functions.to_binary(col, format=None)[source]#

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.

Added in version 3.5.0.

Parameters:
colColumn or str

Input column or strings. A column that evaluates to a string.

formatColumn or str, optional

format to use to convert binary values. A column that evaluates to a string. Must be a constant.

Examples

Example 1: Convert string to a binary with encoding specified

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([("abc",)], ["e"])
>>> df.select(sf.try_to_binary(df.e, sf.lit("utf-8")).alias('r')).collect()
[Row(r=b'abc')]

Example 2: Convert string to a timestamp without encoding specified

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([("414243",)], ["e"])
>>> df.select(sf.try_to_binary(df.e).alias('r')).collect()
[Row(r=b'ABC')]