Skip to content

ENH: improve error message for ragged-array creation failure #13913

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

Merged
merged 1 commit into from
Jul 15, 2019

Conversation

mattip
Copy link
Member

@mattip mattip commented Jul 4, 2019

Fixes #5303, #6584 for the case where np.array gets a ragged-sequence input and a dtype is specified. This does not try to fix the case for where no dtype is specified.

Previously np.array([[1], [2, 3]], dtype=int) would emit a "setting an array element with a sequence" message, now changed to "cannot create an array from unequal-length (ragged) sequences". This also is the error for structured dtypes where previously the error was "TypeError: expected a readable buffer object"

Test added.

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic seems right (I cannot see how it can fail), tried with structured dtypes just to be sure. Matti said that the approach to move the check out of this function turned out less nice. So I am in favor as a small but clear improvement at least in some cases. If we add something more broadly can still remove it again.

@seberg seberg merged commit 2870105 into numpy:master Jul 15, 2019
@QuLogic
Copy link
Contributor

QuLogic commented Jul 26, 2019

A related effect of this change is that assigning a ragged list (but still of correct number of values) to a slice no longer works, as shown in this example, extracted from Matplotlib's tests:

points = np.array([[1, 2], [3, 6]])

xlim = points[:, 0]
ylim = points[:, 1]
edge_size = max(np.diff(xlim), np.diff(ylim))

interval = [xlim[0], xlim[0] + edge_size]

points[:, 0] = interval

fails with a ValueError because the first element is a scalar and the second element is an array of length one.

I wrote a fix for this, but a question did come up for whether this was an intended change?

@seberg
Copy link
Member

seberg commented Jul 26, 2019

Oh, I am surprised our tests did not find that, thanks, sounds like I have yo look much closet and possibly revert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

the error message for accidentally irregular arrays is confusing
3 participants