[Numpy-discussion] Problem with concatenate and object arrays

Robert Kern robert.kern at gmail.com
Sun Sep 3 16:54:06 CDT 2006


Charles R Harris wrote:
> On 9/3/06, *Fernando Perez* <Fernando.Perez at colorado.edu 
> <mailto:Fernando.Perez at colorado.edu>> wrote:
> 
>     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
> 
> 
> I think it's propably a bug:
> 
>  >>> concatenate((array([]),b))
> array([None, None], dtype=object)

Well, if you can fix it without breaking anything else, then it's a bug.

However, I would suggest that a rule of thumb for using object arrays is to 
always be explicit. Never rely on automatic conversion from Python containers to 
object arrays. Since Python containers are also objects, it is usually ambiguous 
what the user meant.

I kind of liked numarray's choice to move the object array into a separate 
constructor. I think that gave them some flexibility to choose different syntax 
and semantics from the generic array() constructor. Since constructing object 
arrays is so different from constructing numeric arrays, I think that difference 
is warranted.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco





More information about the Numpy-discussion mailing list