numpy.dtype.newbyteorder#

method

dtype.newbyteorder(new_order='S', /)#

Return a new dtype with a different byte order.

Changes are also made in all fields and sub-arrays of the data type.

Parameters:
new_orderstring, optional

Byte order to force; a value from the byte order specifications below. The default value (『S』) results in swapping the current byte order. new_order codes can be any of:

  • 『S』 - swap dtype from current to opposite endian

  • {『<』, 『little』} - little endian

  • {『>』, 『big』} - big endian

  • {『=』, 『native』} - native order

  • {『|』, 『I』} - ignore (no change to byte order)

Returns:
new_dtypedtype

New dtype object with the given change to the byte order.

Notes

Changes are also made in all fields and sub-arrays of the data type.

Examples

>>> import sys
>>> sys_is_le = sys.byteorder == 'little'
>>> native_code = '<' if sys_is_le else '>'
>>> swapped_code = '>' if sys_is_le else '<'
>>> import numpy as np
>>> native_dt = np.dtype(native_code+'i2')
>>> swapped_dt = np.dtype(swapped_code+'i2')
>>> native_dt.newbyteorder('S') == swapped_dt
True
>>> native_dt.newbyteorder() == swapped_dt
True
>>> native_dt == swapped_dt.newbyteorder('S')
True
>>> native_dt == swapped_dt.newbyteorder('=')
True
>>> native_dt == swapped_dt.newbyteorder('N')
True
>>> native_dt == native_dt.newbyteorder('|')
True
>>> np.dtype('<i2') == native_dt.newbyteorder('<')
True
>>> np.dtype('<i2') == native_dt.newbyteorder('L')
True
>>> np.dtype('>i2') == native_dt.newbyteorder('>')
True
>>> np.dtype('>i2') == native_dt.newbyteorder('B')
True