diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2020-09-02 11:02:52 -0500 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2020-09-02 11:02:52 -0500 |
commit | 22d0b20fd096348bd74e30669ce19fefb73554f8 (patch) | |
tree | 100b31a4cd7f96d9dd36e6c3cf7b71c795075911 | |
parent | db866874ccb8c8c814df57eb1edca4fd05eb1753 (diff) | |
download | numpy-22d0b20fd096348bd74e30669ce19fefb73554f8.tar.gz |
BUG: Fix dimension discovery of within array ragged cases
When `np.array(...)` finds a shape mismatch while inspecting
an array (or array-like), the current logic is to use only
part of those dimensions (as many as possible).
This case was buggy in the new code, and is now fixed.
In general, we may want to change the logic here, e.g. to
always strip all dimensions of that array (allowing an object
array of arrays), but there are some corner cases where the
current behaviour somewhat "works", so this fixes a bug while
gh-17224 and the issues around it remain independently.
-rw-r--r-- | numpy/core/src/multiarray/array_coercion.c | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_array_coercion.py | 21 |
2 files changed, 22 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index ffb5bd632..3f3fd1387 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -548,7 +548,7 @@ update_shape(int curr_ndim, int *max_ndim, success = -1; if (!sequence) { /* Remove dimensions that we cannot use: */ - *max_ndim -= new_ndim + i; + *max_ndim -= new_ndim - i; } else { assert(i == 0); diff --git a/numpy/core/tests/test_array_coercion.py b/numpy/core/tests/test_array_coercion.py index d18df2e9c..22f296f85 100644 --- a/numpy/core/tests/test_array_coercion.py +++ b/numpy/core/tests/test_array_coercion.py @@ -11,6 +11,7 @@ from itertools import product import numpy as np from numpy.core._rational_tests import rational +from numpy.core._multiarray_umath import _discover_array_parameters from numpy.testing import ( assert_array_equal, assert_warns, IS_PYPY) @@ -478,6 +479,26 @@ class TestNested: with pytest.raises(ValueError): np.array([[], np.empty((0, 1))], dtype=object) + def test_array_of_different_depths(self): + # When multiple arrays (or array-likes) are included in a + # sequences and have different depth, we currently discover + # as many dimensions as they share. (see also gh-17224) + arr = np.zeros((3, 2)) + mismatch_first_dim = np.zeros((1, 2)) + mismatch_second_dim = np.zeros((3, 3)) + + dtype, shape = _discover_array_parameters( + [arr, mismatch_second_dim], dtype=np.dtype("O")) + assert shape == (2, 3) + + dtype, shape = _discover_array_parameters( + [arr, mismatch_first_dim], dtype=np.dtype("O")) + assert shape == (2,) + # The second case is currently supported because the arrays + # can be stored as objects: + res = np.asarray([arr, mismatch_first_dim], dtype=np.dtype("O")) + assert res[0] is arr + assert res[1] is mismatch_first_dim class TestBadSequences: # These are tests for bad objects passed into `np.array`, in general |