[Numpy-discussion] byteswap() leaves dtype unchanged

Erin Sheldon erin.sheldon at gmail.com
Wed May 30 09:33:25 EDT 2007


On 5/30/07, Erin Sheldon <erin.sheldon at gmail.com> wrote:
> 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.

By "numbers have still changed" I mean the underlying byte order is
still different, but you can now use the object for mathematical
operations.  My point is that the metadata for the object should be
correct after using it's built-in methods.

>
>
>
> >
> > 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
> >
>



More information about the NumPy-Discussion mailing list