This behavior has been discussed several times before (for example, Override dict with numpy support ). np.array trying to make as large a dimension array as possible. A model example is nested lists. If it can iterate, and the followers are equal in length, it will βdrillβ down.
Here he went down to 2 levels before meeting with lists of different lengths:
In [250]: np.array([[[1,2],[3]],[1,2]],dtype=object) Out[250]: array([[[1, 2], [3]], [1, 2]], dtype=object) In [251]: _.shape Out[251]: (2, 2)
Without the shape or ndmax parameter, he does not know if I want it to be (2,) or (2,2) . Both will work with dtype.
Compiled code, so itβs not entirely accurate to see what tests it uses. He tries to iterate over lists and tuples, but not over sets or dictionaries.
The surest way to create an array of objects with a given size is to start from empty and fill it
In [266]: A=np.empty((2,3),object) In [267]: A.fill([[1,'one']]) In [276]: A[:]={1,2} In [277]: A[:]=[1,2] # broadcast error
Another way is to start with at least one other element (for example, a None ), and then replace it.
A more primitive ndarray creator is ndarray :
In [280]: np.ndarray((2,3),dtype=object) Out[280]: array([[None, None, None], [None, None, None]], dtype=object)
But this is basically the same as np.empty (unless I give it a buffer).
These are inventions, but they are not expensive (temporary).
================== (edit)
https://github.com/numpy/numpy/issues/5933 , Enh: Object array creation function. is an extension request. Also https://github.com/numpy/numpy/issues/5303 the error message for accidentally irregular arrays is confusing .
The idea of ββthe developer seems preferable to a separate function for creating arrays of dtype=object , with great control over the initial sizes and depth of iteration. They can even leverage error checking to keep np.array from creating irregular arrays.
Such a function can detect the shape of a regular nested iterable to a specified depth and build an array of the type of object to be filled.
def objarray(alist, depth=1): shape=[]; l=alist for _ in range(depth): shape.append(len(l)) l = l[0] arr = np.empty(shape, dtype=object) arr[:]=alist return arr
With various depths:
In [528]: alist=[[Test([1,2,3])], [Test([3,2,1])]] In [529]: objarray(alist,1) Out[529]: array([[Test([1, 2, 3])], [Test([3, 2, 1])]], dtype=object) In [530]: objarray(alist,2) Out[530]: array([[Test([1, 2, 3])], [Test([3, 2, 1])]], dtype=object) In [531]: objarray(alist,3) Out[531]: array([[[1, 2, 3]], [[3, 2, 1]]], dtype=object) In [532]: objarray(alist,4) ... TypeError: object of type 'int' has no len()