-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Inconsistency in assignment to object array. #6074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Well, in its own twisted way, it actually is consistent. Unlike the other two, |
You could look it the other way around. If this was not the case, I do not think there would be any possibility to put an array into another array! ;) |
Yeah, the third one is a level deeper than the other two. Whatever the proper behavior is, it should NOT be the same as the other two. See #6070, which suggests that e.g. |
I was also thinking about consistency with respect to numerical arrays, since for them we have the following behaviour: >>> x = np.zeros((1,1), int)
>>> a = np.ones((), int)
>>> x[0,0] = a
>>> x
array([[1]]) Anyways, I had not thought of putting an ndarray inside an ndarray as a valid use-case. If that is so then this issue might simply be a feature. I think, however, that when ndarrays start to nest like that it is usually unwanted. With respect to @leewz's comment, I used |
Yes, though you could argue that the way you get there is at least in principle reversed. It is not so much that the right hand side takes the element from the left hand side in the assignment. It is that the left hand side says that it can be interpreted as an integer (since it is 0-d). |
Just to add to comments above: Assigning to object arrays is a special case that behaves differently from non-object arrays, specifically to allow nesting of ndarrays. For example, this special case is explicit in this code. See also http://stackoverflow.com/questions/3273040/numpy-object-array-of-numerical-arrays This is one of a couple "special" things about object arrays I think should be documented. (Another is the "special" casting rules that apply to object arrays). I think a little dedicated section on object arrays in the user docs would be good. Probably it should wait until some issues related to object arrays are decided though. |
I think it's safe to close this, then. I guess the simplest workaround to get the behavior I want is to always include the ellipsis for item assignment. Object arrays are tricky to work with but can help a great deal when working with sympy objects, for example. |
Here's a thing that seems inconsistent to me: >>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[1, 2, 3], [4, 5, 6]])
>>> np.array([a, b])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (2,2) into shape (2) but >>> np.array([ [[1, 2], [3, 4]], [[1, 2, 3], [4, 5, 6]] ])
array([[[1, 2], [3, 4]],
[[1, 2, 3], [4, 5, 6]]], dtype=object) It seems inconsistent that one creates an object array, but the other one does not (and gives an error instead). |
Honestly, I prefer it that way. Numpy is for numbers. If you want something weird, you should have to explicitly say so. It should give you an error if you accidentally ask for something weird, and I can see that being an accident. |
Consider the following code:
As it can be seen, assignment of a zero-rank array to an object array causes nesting of array inside an array. I believe that the third case (assignment to
z
) should behave like the other cases, otherwise one can quickly generate arrays inside arrays inside arrays and necessitate extra code to treat special cases.This was tested in numpy 1.9.2 under python 3.4.2.
The text was updated successfully, but these errors were encountered: