User guide

NumPy User Guide, Release 1.9.0
Note the the array has not changed in memory:
>>> fixed_end_dtype_arr.tobytes() == big_end_str
True
Data and type endianness don’t match, change data to match dtype
You might want to do this if you need the data in memory to be a certain ordering. For example you might be writing
the memory out to a file that needs a certain byte ordering.
>>> fixed_end_mem_arr = wrong_end_dtype_arr.byteswap()
>>> fixed_end_mem_arr[0]
1
Now the array has changed in memory:
>>> fixed_end_mem_arr.tobytes() == big_end_str
False
Data and dtype endianness match, swap data and dtype
You may have a correctly specified array dtype, but you need the array to have the opposite byte order in memory, and
you want the dtype to match so the array values make sense. In this case you just do both of the previous operations:
>>> swapped_end_arr = big_end_arr.byteswap().newbyteorder()
>>> swapped_end_arr[0]
1
>>> swapped_end_arr.tobytes() == big_end_str
False
An easier way of casting the data to a specific dtype and byte ordering can be achieved with the ndarray astype method:
>>> swapped_end_arr = big_end_arr.astype(’<i2’)
>>> swapped_end_arr[0]
1
>>> swapped_end_arr.tobytes() == big_end_str
False
2.7 Structured arrays (aka “Record arrays”)
2.7.1 Structured Arrays (and Record Arrays)
Introduction
Numpy provides powerful capabilities to create arrays of structs or records. These arrays permit one to manipulate the
data by the structs or by fields of the struct. A simple example will show what is meant.:
>>> x = np.zeros((2,),dtype=(’i4,f4,a10’))
>>> x[:] = [(1,2.,’Hello’),(2,3.,"World")]
>>> x
array([(1, 2.0, ’Hello’), (2, 3.0, ’World’)],
dtype=[(’f0’, ’>i4’), (’f1’, ’>f4’), (’f2’, ’|S10’)])
2.7. Structured arrays (aka “Record arrays”) 31