NumPy ndarray.byteswap()
The numpy.ndarray.byteswap()
method swaps the byte order of an array’s elements. This is useful when dealing with different endianness (byte order) between systems.
Syntax
ndarray.byteswap(inplace=False)
Parameters
Parameter | Type | Description |
---|---|---|
inplace | bool, optional | If True , byteswap is performed in-place, modifying the original array. If False (default), a new byteswapped array is returned. |
Return Value
Returns a byteswapped version of the array if inplace=False
. If inplace=True
, the operation modifies the original array and returns it.
Examples
1. Byteswapping an Integer Array
In this example, we create an integer NumPy array and apply the byteswap()
method to swap the byte order.
import numpy as np
# Creating an array of integers
arr = np.array([1, 256, 65536], dtype=np.int32)
# Display original array and its byte representation
print("Original array:", arr)
print("Original array bytes:\n", arr.tobytes())
# Applying byteswap()
swapped_arr = arr.byteswap()
# Display swapped array and its byte representation
print("Byteswapped array:", swapped_arr)
print("Byteswapped array bytes:\n", swapped_arr.tobytes())
Output:
Original array: [ 1 256 65536]
Original array bytes:
b'\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00'
Byteswapped array: [16777216 65536 256]
Byteswapped array bytes:
b'\x00\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00'
Each integer’s byte representation is reversed in the swapped array, affecting the numerical values.
2. Using inplace=True
for Modifying the Original Array
Setting inplace=True
modifies the original array instead of returning a new one.
import numpy as np
# Creating an array of unsigned integers
arr = np.array([1, 512, 131072], dtype=np.uint32)
# Display original array
print("Original array:", arr)
# Byteswap in place
arr.byteswap(inplace=True)
# Display modified array
print("Modified array after inplace byteswap:", arr)
Output:
Original array: [ 1 512 131072]
Modified array after inplace byteswap: [ 16777216 131072 1]
Since inplace=True
, the original array is modified instead of creating a new one.
3. Byteswapping a Floating-Point Array
Floating-point numbers also have a byte order that can be swapped using byteswap()
.
import numpy as np
# Creating an array of floats
arr = np.array([1.5, 256.75, -10.25], dtype=np.float64)
# Display original array and its byte representation
print("Original array:", arr)
print("Original array bytes:\n", arr.tobytes())
# Byteswap the array
swapped_arr = arr.byteswap()
# Display swapped array and its byte representation
print("Byteswapped array:", swapped_arr)
print("Byteswapped array bytes:\n", swapped_arr.tobytes())
Output:
Original array: [ 1.5 256.75 -10.25]
Original array bytes:
b'\x00\x00\x00\x00\x00\x00\xf8?\x00\x00\x00\x00\x00\x0cp@\x00\x00\x00\x00\x00\x80$\xc0'
Byteswapped array: [3.139837e-319 4.027465e-318 4.149171e-317]
Byteswapped array bytes:
b'?\xf8\x00\x00\x00\x00\x00\x00@p\x0c\x00\x00\x00\x00\x00\xc0$\x80\x00\x00\x00\x00\x00'
Byteswapping changes the byte representation, which may result in unintended values for floating-point numbers.
4. Checking the Endianness of an Array
We can check whether an array is in little-endian or big-endian format using the dtype.byteorder
attribute.
import numpy as np
# Creating an array of integers
arr = np.array([1, 2, 3], dtype=np.int16)
# Checking byte order
print("Byte order before swapping:", arr.dtype.byteorder)
# Byteswap the array
swapped_arr = arr.byteswap()
# Checking byte order after swapping
print("Byte order after swapping:", swapped_arr.dtype.byteorder)
Output:
Byte order before swapping: =
Byte order after swapping: =
The '='
symbol indicates that the array uses the native byte order. After byteswapping, the array still reports the same byte order, but its values are stored differently.