question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"setting an array element with a sequence" error could be improved.

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:87
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
argriffingcommented, Oct 30, 2015

Re-opening in light of https://github.com/numpy/numpy/issues/5303#issuecomment-152354367.

There are a few different but related issues. One issue is explicit vs. implicit dtype=object array creation. The github issue for that is https://github.com/numpy/numpy/issues/5353 (also https://github.com/numpy/numpy/issues/6070, I see). Another issue is the possibility to create ndarrays of objects with conformable shapes without automatically peeking into those objects and trying to figure out what the user wants. That issue is https://github.com/numpy/numpy/issues/5933.

We could keep this issue open, for improving the error message for specific cases like np.asarray([[1, 2], [2, 3, 4]], dtype=np.float). If the error message could be special-cased for np.asarray(a, dtype=b) even for really specific kinds of a and b, this could be useful. For example, maybe a special case could apply when a is a Python list recursively containing more lists and numeric values (and maybe only when the all leaves of the tree defined by the list all share the same depth?) and where b is some simple numeric dtype.

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.
1reaction
Chokerinocommented, Sep 20, 2020

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 ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).. I am trying to do a machine learning task and my current dataset is a numpy array of shape (556,1) and for every sample it have 27 features so (27,1) and each feature has variable length and the same feature might have a different shape for different samples. Any help on how I get fix this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix: ValueError: setting an array element with a ...
Here we have seen that this error is cause because we are assigning array as a element to array which accept string data-type....
Read more >
ValueError: setting an array element with a sequence
Possible reason 1: trying to create a jagged array. You may be creating an array from a list that isn't shaped like a...
Read more >
[FIXED] ValueError: setting an array element with a sequence
Solution: To solve this error, you have to set the data type (dtype) ...
Read more >
ValueError: setting an array element with a sequence - STechies
While programming in Python, especially Numpy a library in Python, programmers encounter an error called ValueError: setting an array element with a sequence....
Read more >
Valueerror Setting an Array Element With a Sequence.: Fix It ...
The easiest way to fix the valueerror setting an array element with a sequence code is to remove the arrays with different dimensions....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found