-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: Fix double-wrapping of object scalars #8643
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
Conversation
(since you reviewed and merged #8441) |
numpy/lib/shape_base.py
Outdated
@@ -134,9 +134,9 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs): | |||
buff = res.__array_prepare__(buff) | |||
|
|||
# save the first result, then compute and save all remaining results | |||
buff[ind0] = res | |||
buff[ind0] = res[()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize that indexing like this was valid on non-scalar arrays. This needs a comment, or better yet, maybe a helper function. Something like asanyarray_or_scalar
? This is related to #5353, though in this case we want fallback behavior.
Alternatively, maybe we should change indexing behavior for cases like this in general? I don't see any situation in which a user would want an object array of scalar object arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure is possible, though you must be sure you have an array. Otherwise, object dtype might blow up in your face.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize that indexing like this was valid on non-scalar arrays
Yep, in these cases it's just a no-op, as if it were :
, but repeated 0 times.
I'll add the comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps asanyarray(or_scalar=True)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, comment added. I think that a helper function would be more widely applicable, so belongs in a separate MAINT PR. I've created #8681 to discuss what it should be named
7b6d2f7
to
31d00e3
Compare
Tangent idea: Maybe we should define a new helper method to convert 0d arrays to scalars. In C we have I think the current workaround in python using empty tuples is a bit opaque, and there are weird edge cases involving it. See #7267, or this comment |
This isn't quite the same problem as #7267, as here we know we have a 0d array and not a scalar. And that comment you link to is no longer applicable |
I agree #7267 is not a problem here, I should have said so more clearly. I didn't realize that comment was no longer applicable... I guess empty tuple behavior changed pretty recently... |
@ahaldane : Perhaps a regression in 1.12? I'm testing on 1.10.4, 1.11.1, and master, and all are fine |
Hmm maybe that comment was just wrong, I also don't notice any changes going back to 1.9.3. |
numpy/lib/shape_base.py
Outdated
for ind in inds: | ||
buff[ind] = asanyarray(func1d(inarr_view[ind], *args, **kwargs)) | ||
buff[ind] = asanyarray(func1d(inarr_view[ind], *args, **kwargs))[()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also have been fixed by changing this line to buff[ind + np.index_exp[...]]
, and leaving the rest untouched. Probably better for not going through a scalar, and avoiding #7267?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is definitely the right approach, in light of #8902. Updating...
eb73409
to
b3d7e69
Compare
Adding this to the milestone, since it would be a shame to introduce a feature already broken |
I wondered slightly about adding So, good to go as far as I'm concerned. |
Remember that these indices are used for input and output |
Duh, should have seen that. |
@shoyer, happy with the new implementation? Digging a little deeper, the rules for object array nesting seem to be:
I think these rules make sense, as they allow you to choose between nesting arrays and not doing so, although perhaps need more documentation elsewhere (but that's well out of scope for this PR). Worth noting that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me. @eric-wieser please go ahead and merge!
Fixes #8642