[Numpy-discussion] bug in object arrays:

Todd Miller jmiller at stsci.edu
Tue Jun 8 12:37:38 EDT 2004


On Sun, 2004-06-06 at 12:10, Peter Verveer wrote:
> Is this a bug?

I think this is more of a bonanza:  a bug and an ambiguity.

> This should result in an array of numpy arrays, but it does give an  
> error:
> 
>  >>> a = array([1,2])
>  >>> b = array([3,4])
>  >>> c = objects.array([a,b])
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>    File  
> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
> python2.3/site-packages/numarray/objects.py", line 732, in array
>      return fromlist(sequence, shape)
>    File  
> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
> python2.3/site-packages/numarray/objects.py", line 755, in fromlist
>      return ObjectArray(objects=l, shape=shape)
>    File  
> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
> python2.3/site-packages/numarray/objects.py", line 506, in __init__
>      oshape = _shapeFromNestedSequence(objects)
>    File  
> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
> python2.3/site-packages/numarray/objects.py", line 289, in  
> _shapeFromNestedSequence
>      return [len(s)] + _shapeFromNestedSequence(s[0])
> TypeError: can only concatenate list (not "tuple") to list

The bug is that your example didn't work as follows:

>>> import numarray.objects as obj
>>> obj.array([a,b])
ObjectArray([[1, 2],
             [3, 4]])

Returning a shape=(2,) object array is another obvious behavior but
turns out not to be what Numeric does and hence not what numarray does
either.   Faced with sequence objects,  Numeric and numarray both just
keep recursing (logically anyway) until they hit something which isn't a
sequence.  Thus, they return not a 1D array of arrays, but a 2D array of
numbers:

>>> import Numeric
Numeric.array([a,b], typecode='O')
array([[1 , 2 ],
       [3 , 4 ]],'O')

With some new code I added today,  numarray of the future will support
your preferred behavior like this, barring further discussion:

>>> obj.array([a,b], rank=1)
ObjectArray([array([1, 2]), array([3, 4])])

Here, the new "rank" parameter explicitly specifies the expected rank of
the resulting array, defining the point at which nested sequences become
"objects in their own right".  Perry's inclination was that rank should
default to 1 so that your expected behavior was the default.  Since
that's not backward compatible with Numeric (or numarray-0.9) I think
maybe rank=None is better.  In this case, rank=None is equivalent to
rank=2.

How does the rank parameter sound?

Regards,
Todd






More information about the NumPy-Discussion mailing list