[Numpy-discussion] improving structured array assignment

Allan Haldane allanhaldane at gmail.com
Thu Aug 6 22:59:15 EDT 2015


Hello all,

I've written up a tentative PR which tidies up structured array assignment,

https://github.com/numpy/numpy/pull/6053

It has a backward incompatible change which I'd especially like to get 
some feedback on: Structure assignment now always works "by field 
position" instead of "by field name". Consider the following assignment:

   >>> v1 = np.array([(1,2,3)],
   ...               dtype=[('a', 'i4'), ('b', 'i4'), ('c', 'i4')])
   >>> v2 = np.array([(4,5,6)],
   ...               dtype=[('b', 'i4'), ('a', 'i4'), ('c', 'i4')])
   >>> v1[:] = v2

Previously, v1 would be set to "(5,4,6)" but with the PR it is set to 
"(4,5,6)".

This might seem like negligible improvement, but assignment "by field 
name" has lots of inconsistent/broken edge cases which I've listed in 
the PR, which disappear with assignment "by field position". The PR 
doesn't seem to break much of anything in scipy, pandas, and astropy.

If possible, I'd like to try getting a deprecation warning for this 
change into 1.10.

I also changed a few more minor things about structure assignment, 
expanded the docs on structured arrays, and made a multi-field index 
(arr[['f1', 'f0']]) return a view instead of a copy, which had been 
planned for 1.10 but didn't get in because of the strange behavior of 
structure assignment.

Allan



More information about the NumPy-Discussion mailing list