[Numpy-discussion] Improved creation of object arrays

Travis Oliphant oliphant.travis at ieee.org
Wed Jul 19 02:57:22 CDT 2006

I made a small change to the array creation function so that if you 
explicitly specify dtype=object, then the logic for determining the 
shape of the array is changed.

The new logic for constructing dtype=object arrays from Python sequences 
is that the shape is determined by nested lists (and only lists).  The 
nesting must be consistent or else the elements of the last conforming 
lists are assumed to be objects.

Here are some examples:

array([[1,2],3,4],dtype=object)  returns a 1-d array of shape (3,)

array([[1,2],[3,4]],dtype=object)  returns a 2-d array of shape (2,2)

array([(1,2),(3,4)],dtype=object) returns a 1-d array of shape (2,)

array([],dtype=object) returns a 0-d array of shape ()

array([[],[],[]],dtype=object) returns a 1-d array of shape (3,)

array([[3,4],[5,6],None],dtype=object) returns a 1-d array of shape (3,)

You have to actually specify dtype=object to get this, otherwise the old 
code-path will be taken.


More information about the Numpy-discussion mailing list