[Numpy-discussion] byteorder question
Travis Oliphant
oliphant at ee.byu.edu
Fri Jun 30 19:01:10 EDT 2006
Jonathan Taylor wrote:
> In some earlier code (at least one of) the following worked fine. I
> just want
> to get a new type that is a byteswap of, say, float64 because I want to
> memmap an array with a non-native byte order.
>
> Any suggestions?
Last year the array scalars (like float64) were confused with the
data-type objects dtype('=i4'). This was fortunately changed many
months ago so the two are now separate concepts. This may be why your
old code worked.
You want to get a data-type object itself:
d = numpy.dtype(numpy.float64)
d = numpy.float64(1).dtype # you have to instantiate a float64 object
to access it's data-type.
Then
d.newbyteorder('>') or d.newbyteorder('big') will work.
But, probably easier and clearer is just to use:
dlittle = numpy.dtype('<f8')
dbig = numpy.dtype('>f8')
There are now full-fledged data-type objects in NumPy. These can be
used everywhere old typecodes were used. In fact, all other aliases get
converted to these data-type objects because they are what NumPy needs
to construct the ndarray.
These data-type objects are an important part of the basearray concept
being introduced to Python, so education about them is very timely.
They are an out-growth of the PyArray_Descr * structure that Numeric
used to "represent" a data-type internally. Basically , the old
PyArray_Descr * structure was enhanced and given an Object header.
Even just getting these data-type objects into Python would be a useful
first-step to exchanging data.
For NumPy, the data-type objects have enabled very sophisticated
data-type specification and are key to record-array support in NumPy.
Best,
-Travis
More information about the NumPy-Discussion
mailing list