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. - New in version 3.5.0. - Parameters
 - 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=bytearray(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=bytearray(b'ABC'))]