8000 BUG: Make np.array([[1], 2]) and np.array([1, [2]]) behave in the same way by eric-wieser · Pull Request #11601 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Make np.array([[1], 2]) and np.array([1, [2]]) behave in the same way #11601

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 4 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
65 changes: 33 additions & 32 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
int *out_is_object)
{
PyObject *e;
int r;
npy_intp n, i;
Py_buffer buffer_view;
PyObject * seq;
Expand Down Expand Up @@ -846,46 +845,48 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
return 0;
}
else {
npy_intp dtmp[NPY_MAXDIMS];
int j, maxndim_m1 = *maxndim - 1;
e = PySequence_Fast_GET_ITEM(seq, 0);

r = discover_dimensions(e, &maxndim_m1, d + 1, check_it,
stop_at_string, stop_at_tuple,
out_is_object);
if (r < 0) {
int all_elems_maxndim = *maxndim - 1;
npy_intp *all_elems_d = d + 1;
int all_dimensions_match = 1;

/* Get the dimensions of the first item as a baseline */
PyObject *first = PySequence_Fast_GET_ITEM(seq, 0);
if (discover_dimensions(
first, &all_elems_maxndim, all_elems_d, check_it,
stop_at_string, stop_at_tuple, out_is_object) < 0) {
Py_DECREF(seq);
return r;
return -1;
}

/* For the dimension truncation check below */
*maxndim = maxndim_m1 + 1;
/* Compare the dimensions of all the remaining items */
for (i = 1; i < n; ++i) {
e = PySequence_Fast_GET_ITEM(seq, i);
/* Get the dimensions of the first item */
r = discover_dimensions(e, &maxndim_m1, dtmp, check_it,
stop_at_string, stop_at_tuple,
out_is_object);
if (r < 0) {
int j;
int elem_maxndim = *maxndim - 1;
npy_intp elem_d[NPY_MAXDIMS];

PyObject *elem = PySequence_Fast_GET_ITEM(seq, i);
if (discover_dimensions(
elem, &elem_maxndim, elem_d, check_it,
stop_at_string, stop_at_tuple, out_is_object) < 0) {
Py_DECREF(seq);
return r;
return -1;
}

/* Reduce max_ndim_m1 to just items which match */
for (j = 0; j < maxndim_m1; ++j) {
if (dtmp[j] != d[j+1]) {
maxndim_m1 = j;
/* Find the number of left-dimensions which match, j */
for (j = 0; j < elem_maxndim && j < all_elems_maxndim; ++j) {
if (elem_d[j] != all_elems_d[j]) {
break;
}
}
if (j != elem_maxndim || j != all_elems_maxndim) {
all_dimensions_match = 0;
}
all_elems_maxndim = j;
}
/*
* If the dimensions are truncated, need to produce
* an object array.
*/
if (maxndim_m1 + 1 < *maxndim) {
*maxndim = all_elems_maxndim + 1;
if (!all_dimensions_match) {
/* typically results in an array containing variable-length lists */
*out_is_object = 1;
*maxndim = maxndim_m1 + 1;
}
}

Expand Down Expand Up @@ -1704,9 +1705,9 @@ PyArray_GetArrayParamsFromObject(PyObject *op,

*out_ndim = NPY_MAXDIMS;
is_object = 0;
if (discover_dimensions(op, out_ndim, out_dims, check_it,
stop_at_string, stop_at_tuple,
&is_object) < 0) {
if (discover_dimensions(
op, out_ndim, out_dims, check_it,
stop_at_string, stop_at_tuple, &is_object) < 0) {
Py_DECREF(*out_dtype);
if (PyErr_Occurred()) {
return -1;
Expand Down
31 changes: 31 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ def test_overlapping_assignment(self):


class TestCreation(object):
"""
Test the np.array constructor
"""
def test_from_attribute(self):
class x(object):
def __array__(self, dtype=None):
Expand Down Expand Up @@ -903,6 +906,34 @@ def test_array_too_big(self):
assert_raises(ValueError, np.ndarray, buffer=buf, strides=(0,),
shape=(max_bytes//itemsize + 1,), dtype=dtype)

def test_jagged_ndim_object(self):
# Lists of mismatching depths are treated as object arrays
a = np.array([[1], 2, 3])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)

a = np.array([1, [2], 3])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)

a = np.array([1, 2, [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)

def test_jagged_shape_object(self):
# The jagged dimension of a list is turned into an object array
a = np.array([[1, 1], [2], [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)

a = np.array([[1], [2, 2], [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)

a = np.array([[1], [2], [3, 3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)


class TestStructured(object):
def test_subarray_field_access(self):
Expand Down
0