You need to pass in a bytes object, rather than a str. The typical way to go from str (a unicode string in Python 3) to bytes is to use the .encode() method on the string and specify the encoding you wish to use.

my_bytes = my_string.encode('utf-8')
Answer from Amber on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί working-with-binary-data-in-python
Working with Binary Data in Python - GeeksforGeeks
July 15, 2025 - As seen above it is possible to encode or decode strings and binary strings using the encode() or decode() function. We need the encoding because in some encodings it is not possible to to decode the strings.
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί base64.html
base64 β€” Base16, Base32, Base64, Base85 Data Encodings
Encode the bytes-like object s, which can contain arbitrary binary data, and return bytes containing the base64-encoded data, with newlines (b'\n') inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME).
🌐
Packtpub
subscription.packtpub.com β€Ί book β€Ί data β€Ί 9781804611302 β€Ί 2 β€Ί ch02lvl1sec21 β€Ί performing-binary-encoding
Chapter 2: Encoding Categorical Variables | Python Feature Engineering Cookbook
By the end of this Python book, ... encoding is a categorical encoding technique that uses binary code – that is, a sequence of zeroes and ones – to represent the different categories of the variable....
🌐
IT trip
en.ittrip.xyz β€Ί python
The Essential Guide to Encoding and Decoding Binary Files in Python | IT trip
November 4, 2023 - Python’s `binascii` module is perfect for such operations. import binascii hex_str = binascii.hexlify(binary_data) Encoding and decoding binary files in Python is a versatile skill that’s applicable in many programming scenarios, from data storage to network communication.
🌐
AskPython
askpython.com β€Ί python β€Ί examples β€Ί binary-to-utf8-conversion
How to Convert Binary Data to UTF-8 in Python - AskPython
April 10, 2025 - Computers ultimately store all data as binary – sequences of 1’s and 0’s. This low-level binary data representation is great for storage and transmission efficiency. But binary data in raw form is not human-readable. So we have various encoding schemes that convert binary data to text characters and vice-versa. Here are some examples of binary data you may encounter in Python:
🌐
Analytics Vidhya
analyticsvidhya.com β€Ί home β€Ί what are categorical data encoding methods | binary encoding
What are Categorical Data Encoding Methods | Binary Encoding
May 1, 2025 - Binary encoding efficiently converts categories into binary digits, making it suitable for high cardinality datasets. In contrast, one-hot encoding creates separate binary columns for each category.
Find elsewhere
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί binary.html
Binary Data Services β€” Python 3.14.7 documentation
Some libraries described under Text Processing Services also work with either ASCII-compatible binary formats (for example, re) or all binary data (for example, difflib).
Starred by 79 users
Forked by 48 users
Languages: Python
Top answer
1 of 2
2

Your categorical variable has two levels, so there is no actual difference between dummy-coding vs. simply entering the variable into the analysis. That is, to dummy code you would create one new variable with two values but your original variable is already one variable with two values. Dummy-coding is important for variables with more than two possible values. So, in this case the computer won't consider Pave > Grvl.

But if you have more than two variables then you should use dummy variables.

For your data, you can use pandas.get_dummies() or sklearn's one hot encoder to achieve your result.

2 of 2
0
  1. How to encode?

sklearn.preprocessing provides various classes for this purpose, LabelBinarizer is one of them.

  1. Wouldn't the computer interpret this as Pave > Grvl?

Consider an example, where people prefer Paved house in comparison to Graveled. Then their is a relationship between values and hence it should be treated as something like you have mentioned, other wise it should be independent values(refer next answer).

  1. How does the computer differentiate between binary and integer encoding?

As I mentioned above, if the categorical values have some relationship(as mentioned above), then in such a case it should be integer values(0,1,2 and so on), otherwise it should be binary. Binary representation will help us in presenting as an independent value to ML model (however it doesn't make much sense in this case as you just have 2 values). But consider an example where a feature have more than 2 categorical values. If they all are independent then it should be represented as binary value i.e in the form of OneHotEncoding(refer sklearn.preprocessing classes).

🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί codecs.html
codecs β€” Codec registry and base classes
If encoding is not None, then the underlying encoded files are always opened in binary mode. No automatic conversion of '\n' is done on reading and writing.
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί binascii.html
binascii β€” Convert between binary and ASCII
Convert binary data to a line(s) of ASCII characters in quoted-printable encoding. The return value is the converted line(s). If the optional argument quotetabs is present and true, all tabs and spaces will be encoded.
🌐
Real Python
realpython.com β€Ί python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
March 5, 2025 - Python bytes objects are immutable sequences of unsigned bytes used for handling binary data. The difference between bytes and bytearray is that bytes objects are read-only, while bytearray objects are mutable. You convert a Python string to bytes using the str.encode() method, the bytes() function, or the codecs module.
🌐
O'Reilly
oreilly.com β€Ί library β€Ί view β€Ί python-in-a β€Ί 0596100469 β€Ί ch22s01.html
Encoding Binary Data as Text - Python in a Nutshell, 2nd Edition [Book]
July 14, 2006 - The Python standard library supplies modules that support the standard encodings known as Base 64, Quoted Printable, and UU. The base64 module supports the encoding specified in RFC 1521 as Base 64.
Author: Alex Martelli
Published: 2006
Pages: 734
🌐
Coders Tool
coderstool.com β€Ί blog β€Ί what-is-binary-encoding
Binary Encoding Utilities, Tools, and Technical
That is different from general text to binary encoding, but it is still a form of binary representation. In Python, binary encoding usually means converting strings to bytes with an encoding like UTF-8, or formatting numbers as base-2 strings.
🌐
Python Module of the Week
pymotw.com β€Ί 2 β€Ί base64
base64 – Encode binary data into ASCII characters - Python Module of the Week
import base64 original_string = 'This is the data, in the clear.' print 'Original:', original_string encoded_string = base64.b32encode(original_string) print 'Encoded :', encoded_string decoded_string = base64.b32decode(encoded_string) print 'Decoded :', decoded_string Β· $ python base64_base32.py Original: This is the data, in the clear.
🌐
Medium
medium.com β€Ί @kevin.menear β€Ί comparing-label-encoding-one-hot-encoding-and-binary-encoding-for-handling-categorical-variables-933544ccbd02
Comparing Label Encoding, One-Hot Encoding, and Binary Encoding for Handling Categorical Variables in Machine Learning | by Kevin Menear | Medium
February 8, 2023 - Binary encoding can handle categorical variables with a large number of categories more efficiently than one-hot encoding, as it reduces the dimensionality of the data by representing each category with a fixed number of bits.