numpy.diagonal returning read-only copy even in version 1.10

5

I'm doing a program in Python where I need to change the diagonal values of an array. The following code snippet should be sufficient to understand the general idea (it changes to% w_ of% the value in the first position of the main diagonal of the array):

import numpy as np
x = np.eye(2)
d = x.diagonal()
d[0] = 2

According to documentation , in version 1.9 the return of this function had passed to be read only, but this was undone in version 1.10 (which supposedly returns a reference to the original array):

  

In versions of NumPy prior to 1.7, this function has always returned a   new, independent array containing a copy of the values in the   diagonal.

     In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal,   but depending on this fact is deprecated. Writing to the resulting   array continues to work as it is, but FutureWarning is issued.

     In NumPy 1.9 it returns a read-only view on the original array.   Attempting to write to the resulting array will produce an error.

     In NumPy 1.10, it will return the read / write view and writing to the   returned array will alter your original array. The returned array will   have the same type as the input array.

Well, I had version 1.9 installed here (Windows 8) but I just updated it (using 2 ). Guaranteedly the current version is the correct one:

>>> np.version.version
'1.10.1'

However, even then, the sample code above generates the following error:

>>> import numpy as np
>>> x = np.eye(2)
>>> d = x.diagonal()
>>> d[0] = 2
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    d[0] = 2
ValueError: assignment destination is read-only

There is a lot of discussion about the changes in versions in this thread - just where I got the minimal example previously presented - but I did not find any information that contradicts the documentation.

Can anyone offer some help?

    
asked by anonymous 28.10.2015 / 20:17

1 answer

2
So what happens is that, as you have seen, there is a very big discussion about what this function should return with a number of differing opinions.

If you follow the issue you will see that it has been marked as a task for version 1.11

But to clarify this behavior, just look at the code: in the

01.11.2015 / 08:02