User guide

NumPy User Guide, Release 1.9.0
>>> big_end_str = chr(0) + chr(1) + chr(3) + chr(2)
>>> big_end_str
’\x00\x01\x03\x02’
We might want to use an ndarray to access these integers. In that case, we can create an array around this memory,
and tell numpy that there are two integers, and that they are 16 bit and big-endian:
>>> import numpy as np
>>> big_end_arr = np.ndarray(shape=(2,),dtype=’>i2’, buffer=big_end_str)
>>> big_end_arr[0]
1
>>> big_end_arr[1]
770
Note the array dtype above of >i2. The > means ‘big-endian’ (< is little-endian) and i2 means ‘signed 2-byte
integer’. For example, if our data represented a single unsigned 4-byte little-endian integer, the dtype string would be
<u4.
In fact, why don’t we try that?
>>> little_end_u4 = np.ndarray(shape=(1,),dtype=’<u4’, buffer=big_end_str)
>>> little_end_u4[0] == 1
*
256
**
1 + 3
*
256
**
2 + 2
*
256
**
3
True
Returning to our big_end_arr - in this case our underlying data is big-endian (data endianness) and we’ve set the
dtype to match (the dtype is also big-endian). However, sometimes you need to flip these around.
2.6.2 Changing byte ordering
As you can imagine from the introduction, there are two ways you can affect the relationship between the byte ordering
of the array and the underlying memory it is looking at:
Change the byte-ordering information in the array dtype so that it interprets the undelying data as being in a
different byte order. This is the role of arr.newbyteorder()
Change the byte-ordering of the underlying data, leaving the dtype interpretation as it was. This is what
arr.byteswap() does.
The common situations in which you need to change byte ordering are:
1. Your data and dtype endianess don’t match, and you want to change the dtype so that it matches the data.
2. Your data and dtype endianess don’t match, and you want to swap the data so that they match the dtype
3. Your data and dtype endianess match, but you want the data swapped and the dtype to reflect this
Data and dtype endianness don’t match, change dtype to match data
We make something where they don’t match:
>>> wrong_end_dtype_arr = np.ndarray(shape=(2,),dtype=’<i2’, buffer=big_end_str)
>>> wrong_end_dtype_arr[0]
256
The obvious fix for this situation is to change the dtype so it gives the correct endianness:
>>> fixed_end_dtype_arr = wrong_end_dtype_arr.newbyteorder()
>>> fixed_end_dtype_arr[0]
1
30 Chapter 2. Numpy basics