-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
"setting an array element with a sequence" error could be improved. #6584
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
This is a duplicate of #5303. |
indeed, sorry for the noise. |
Re-opening in light of #5303 (comment). There are a few different but related issues. One issue is explicit vs. implicit We could keep this issue open, for improving the error message for specific cases like Here are a few more cases that give the same error message, but which may or may not be in the scope of this issue. >>> np.array([[[1, 2], [3, 4], [5, 6]], [[1], [2], [3]]], dtype=int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence. >>> np.array([[1, 2, 3], 4], dtype=int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence. >>> np.array([(1, 2), (2, 3, 4)], dtype=int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence. |
Thanks for your summary. I think we are on the same page. |
Here's an analogous example with structured arrays, giving a different error message. >>> np.array([[(1, 1), (2, 2)], [(2, 2), (3, 3), (4, 4)]], dtype=[('foo', '>i4'), ('bar', '>i4')])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a readable buffer object When the lists are conformable there is no error. >>> np.array([[(1, 1), (2, 2), (3, 3)], [(2, 2), (3, 3), (4, 4)]], dtype=[('foo', '>i4'), ('bar', '>i4')])
array([[(1, 1), (2, 2), (3, 3)],
[(2, 2), (3, 3), (4, 4)]],
dtype=[('foo', '>i4'), ('bar', '>i4')]) |
I wanna bump this in sight of the BoF in terms of directions. I think better error messages in general would be great, with this my "favorite" examples. |
Thanks @amueller. This has 31 thumbs up, clearly valuable - tagging for the next release to make sure it doesn't drift down the list again. A new issue/strategy for better error messages in general would also be useful. Could be good to discuss during the sprint. |
I tried to think about others and/or come up with general strategies but drew a blank. Maybe looking at highly voted stack overflow issues might help to find common problems? (Gonna do that for sklearn right now lol). |
Hmm most errors are install errors or this one lol https://stackoverflow.com/search?q=%5Bnumpy%5D+error&tab=votes |
I removed the 1.18.0 milestone. We're not forgetting about this! There are new designs in progress that will affect this issue, but they will not be implemented for 1.18. |
I have a branch, where the first example will give this:
I could try to make it more useful, any suggestions? |
Thanks for working on this, this is already much better! Do you keep the first part for backward compatibility? It's not really meaningful to a user, right? |
Yeah, I thought so, plus there are some stranger paths, where we give this error but do not have the context. Although, I suppose there may not be a need to align those, because in those other paths the message is more logic, because it ends up being things like Thats true, that would be nice. I could give the first inhomogeneous part, but it may be hard to report the element where the mismatch occurred. Maybe something for later, rephrasing I can do now. I also have a "failed shape", but I would need two values for the mismatched dimension, and ideally know where exactly the mismatch was. I won't look at it right now, but I think after my PR is in, it may be possible (or at least easier) to expand the message! |
I think this is already a big improvement, there's definitely a trade-off in how much detail to provide. |
I am going to close the issue now, since gh-16200 is in, and the error is now better. But, I admit there is probably still a lot of room for improvement (if just rephrasing), and that is now much easier to do, so I invite contributions or suggestions! |
I have a 3-D numpy array and im getting the same error if i try to change its dataype to float. Also, if i try using the same data without changing anything with sklearn I get the same error while if I use it with keras, I get |
This is a bit of a nitpick, but I think this would improve user-friendlyness.
Currently
np.asarray([[1, 2], [2, 3, 4]], dtype=np.float)
yields "setting an array element with a sequence."I think it would be more helpful if it yielded something like "Can't create array from lists. Column length %d of column %d doesn't match column length %d."
I imagine the error bubbles up from somewhere deep in the code and makes perfect sense where it is raised. It is just something that comes up a lot, and a more friendly and / or informative message could be helpful. So maybe we could catch it further up and include more information if possible.
The text was updated successfully, but these errors were encountered: