How to get endianness of numpy dtype
Asked Answered
D

2

7

Related to Determine the endianness of a numpy array

Given an array

x = np.arange(3)

I can get the byte order by doing

>>> x.dtype.byteorder
'='

How do I find out if this is big or little endian? I would like to get '<', '>' or '|' as the output, not '='.

To be clear, I am not hung up on what format the information comes in. I just want to know "big endian", "little endian" or "irrelevant", but I don't care if it's "native" or not.

Denitadenitrate answered 9/4, 2018 at 19:40 Comment(3)
I might be missing your question here, but just check the native byteorder with sys.byteorder?Breezy
Maybe this? https://mcmap.net/q/347480/-what-39-s-the-most-pythonic-way-of-determining-endiannessShenashenan
@miradulo. I think you are understanding perfectly. I am very surprised that there is no way to get that from numpy directly, given that numpy has to access that information at some point, presumably.Denitadenitrate
S
14

Probably just check sys.byteorder. Even the numpy.dtype.byteorder examples in the docs use sys.byteorder to determine what's native.

endianness_map = {
    '>': 'big',
    '<': 'little',
    '=': sys.byteorder,
    '|': 'not applicable',
}
Spline answered 9/4, 2018 at 19:49 Comment(2)
In that case, I don't have much choice unfortunately.Denitadenitrate
I was thinking of something more like nativecode = '>' if sys.byteorder == 'big' else '<'; x.dtype.byteorder.replace('=', nativecode)Denitadenitrate
M
2

You can swap the endianness twice to make numpy reveal the true endianness:

dtype_nonnative = dtype.newbyteorder('S').newbyteorder('S')
dtype_nonnative.byteorder
Marrs answered 9/4, 2018 at 19:53 Comment(7)
I think I understand why that happens under the hood, but seriously, whyyyyyyyyy?Denitadenitrate
How about a PR to add a property to dtype to always get <, > or |?Denitadenitrate
That's a surprise, considering that even explicitly constructing dtype('<i4') will produce a byteorder of '=' if native is little-endian. I would consider this a bug.Spline
@Spline A bug in which direction? Explicitly constructed dtypes should product explicit byteorders, or newbyteorder should toggle between = and one of {<, >}?Breezy
@miradulo: I'd expect newbyteorder to produce '=' when the resulting byte order is the same as the native byte order. (The fact that explicit dtype('<i4') produces '=' when native is little-endian is documented, while the newbyteorder behavior isn't.)Spline
@Spline Seems sensible, I didn't notice the comment in the docs there. Thanks!Breezy
Another fun fact: x.newbyteorder().newbyteorder() does not do the same thing, where x is the array, but it does when it is the dtype.Denitadenitrate

© 2022 - 2024 — McMap. All rights reserved.