[Numpy-discussion] byteswap() leaves dtype unchanged

Francesc Altet faltet at carabos.com
Wed May 30 03:42:49 EDT 2007


El dt 29 de 05 del 2007 a les 14:17 -0400, en/na Erin Sheldon va
escriure:
> Hi all -
> 
> I have read some big-endian data and I want to byte swap it to little
> endian.  If I use
>   a.byteswap(True)
> the bytes clearly get swapped, but the dtype is not updated to reflect
> the new data type.  e.g
> 
> [~]|1> a=N.array([2.5, 3.2])
> [~]|2> a.dtype.descr
>    <2> [('', '<f8')]
> [~]|3> a.byteswap(True)
>    <3> array([  5.37543423e-321,  -1.54234871e-180])
> [~]|4> a.dtype.descr
>    <4> [('', '<f8')]
> 
> I expected the dtype to be changed so that the print of the array
> would look the same as before, and mathematical operations would work
> properly. This can be done with:
>   a.dtype = a.dtype.newbyteorder()

Yes. That's the canonical way to do what you want.

> Should this not be performed by byteswap()?

No. From the Travis' "Guide to NumPy":

"""
byteswap ({False})

Byteswap the elements of the array and return the byteswapped array. If
the argument is True, then byteswap in-place and return a reference to
self. Otherwise, return a copy of the array with the elements
byteswapped. The data-type descriptor is not changed so the array will
have changed numbers.
"""

Cheers,

-- 
Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth




More information about the NumPy-Discussion mailing list