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.
🌐
Packtpub
subscription.packtpub.com › book › data › 9781804611302 › 2 › ch02lvl1sec21 › performing-binary-encoding
Chapter 2: Encoding Categorical Variables | Python Feature Engineering Cookbook
Import the required Python library, function, and class: import pandas as pd from sklearn.model_selection import train_test_split from category_encoders.binary import BinaryEncoder
🌐
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.
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).

🌐
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:
🌐
Python
docs.python.org › 3 › library › base64.html
base64 — Base16, Base32, Base64, Base85 Data Encodings
Source code: Lib/base64.py This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. This includes the encodings specifi...
Find elsewhere
🌐
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.
🌐
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.
Starred by 79 users
Forked by 48 users
Languages: Python
🌐
Python
docs.python.org › 3 › library › codecs.html
codecs — Codec registry and base classes
The following codecs provide str to bytes encoding and bytes-like object to str decoding, similar to the Unicode text encodings. Changed in version 3.8: “unicode_internal” codec is removed. The following codecs provide binary transforms: bytes-like object to bytes mappings.
🌐
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).
🌐
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 ... known as Base 64, Quoted Printable, and UU. The base64 module supports the encoding specified in RFC 1521 as Base 64. The Base 64 encoding is a compact way to represent arbitrary binary data as text, without any attempt to produce human-readable results...
Author: Alex Martelli
Published: 2006
Pages: 734
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-categorical-data-to-binary-data-in-python
How to convert categorical data to binary data in Python? - GeeksforGeeks
January 17, 2022 - For example, the integer 5 can be converted into bytes, resulting in a binary representation like b'\x00\x05' or b'\x05', depending on the chosen ... Type Conversion is also known as typecasting, is an important feature in Python that allows ...
🌐
DevGenius
blog.devgenius.io › how-to-use-binary-encoding-to-handle-categorical-variables-in-machine-learning-537c5afb7e77
How to Use Binary Encoding to Handle Categorical Variables in Machine Learning | by Dr. Soumen Atta, Ph.D. | Dev Genius
May 4, 2023 - To use binary encoding in Python, we need to import the necessary libraries. We will use the category_encoders library, which contains the BinaryEncoder class that we will use to encode our categorical variables.