[Numpy-discussion] Problem with concatenate and object arrays

Fernando Perez Fernando.Perez at colorado.edu
Sun Sep 3 14:15:22 CDT 2006


Hi all,

I'm wondering if the following difference in behavior of object arrays should 
be considered a bug.  Let a and b be:

In [21]: a = [0,1]

In [22]: b = [ None, None]

If we concatenate a with an empty list, it works:

In [23]: numpy.concatenate(([],a))
Out[23]: array([0, 1])

But not so for b:

In [24]: numpy.concatenate(([],b))
---------------------------------------------------------------------------
exceptions.ValueError                                Traceback (most recent 
call last)

/home/fperez/<ipython console>

ValueError: 0-d arrays can't be concatenated


This behavior changed recently (it used to work with r2788), and I realize 
it's probably part of all the reworkings of the object arrays which have been 
discussed on the list, and all of whose details I have to admit I haven't 
followed.  But this behavior strikes me as a bit inconsistent, since 
concatenation with a non-empty object array works fine:

In [26]: numpy.concatenate(([None],b))
Out[26]: array([None, None, None], dtype=object)

This is biting us in some code which keeps object arrays, because when 
operations of the kind

N.concatenate((some_list_of_objects[:nn],other_object_array))

are taken and nn happens to be 0, the code just explodes.  In our case, the 
variable nn is a runtime computed quantity that comes from a numerical 
algorithm, for which 0 is a perfectly reasonable value.

Are we just misusing things and is there a reasonable alternative, or should 
this be considered a numpy bug?  The r2788 behavior was certainly a lot less 
surprising as far as our code was concerned.

I realize that one alternative is to wrap everything into arrays:

N.concatenate((N.asarray(some_list_of_objects[:nn]),other_object_array))

Is this the only solution moving forward, or could the previous behavior be 
restored without breaking other areas of the new code/design?

Thanks for any input,

f




More information about the Numpy-discussion mailing list