Skip to content

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

Merged
seberg merged 1 commit into
numpy:masterfrom
mattip:improve-error
Jul 15, 2019
Merged

seberg merged 1 commit into
numpy:masterfrom
mattip:improve-error

Conversation

@mattip

@mattip mattip commented Jul 4, 2019

Copy link
Copy Markdown
Member

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.

@seberg seberg left a comment

Copy link
Copy Markdown
Member

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

QuLogic commented Jul 26, 2019 •

Copy link
Copy Markdown
Contributor

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

seberg commented Jul 26, 2019

Copy link
Copy Markdown
Member

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