[Numpy-discussion] convert between structure arrays with different record orderings?

roger peppe rogpeppe@gmail....
Thu May 28 09:39:01 CDT 2009


sorry, i'm new to the list, and if this is a frequently asked question, please
point me in the right direction.

say, for some reason i've got two numpy structure arrays
that both contain the same fields with the same types
but in a different order, is there a simple way to convert
one to the other type?

i'd have thought that astype() might do the job,
but it seems to ignore the field names entirely, thus
values are lost in the following transcript:

> t1 = np.dtype([('foo', np.float64), ('bar', '?')])
> t2 = np.dtype([('bar', '?'), ('foo', np.float64)])
> a1 = np.array([(123, False), (654, True)], dtype=t1)
> a1.astype(t2)
array([(True, 0.0), (True, 1.0)],
      dtype=[('bar', '|b1'), ('foo', '<f8')])

thanks for any help,


More information about the Numpy-discussion mailing list