[Numpy-discussion] byteswap() leaves dtype unchanged

Matthieu Brucher matthieu.brucher at gmail.com
Wed May 30 09:39:44 EDT 2007


Suppose you get data, but it is byte-swapped. Calling this method would
yeild a correct array. If the type is changed too, it would not be useful :(

Matthieu

2007/5/30, Erin Sheldon <erin.sheldon at gmail.com>:
>
> On 5/30/07, Francesc Altet <faltet at carabos.com> wrote:
> > 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.
> > """
>
> This doesn't make any sense.  The numbers have changed but the dtype
> is now incorrect.  If you byteswap and correct the dtype the numbers
> have still changed, but you now can actually use the object.
>
>
>
> >
> > 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
> >
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion at scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070530/63186691/attachment.html>


More information about the NumPy-Discussion mailing list