[Numpy-discussion] Problem with concatenate and object arrays

Travis Oliphant oliphant.travis at ieee.org
Thu Sep 7 02:03:03 CDT 2006


Charles R Harris wrote:
> OK. I do have a couple of questions. Let me insert the docs for array 
> and asarray :
>
>     """array(object, dtype=None, copy=1,order=None, subok=0,ndmin=0)
>
>     Return an array from object with the specified date-type.
>
> 1) Is it true that array doesn't always return a copy except by 
> default? asarray says it contrasts with array in this regard. Maybe 
> copy=0 should be deprecated.
array is the main creation function.   It is a loose wrapper around 
PyArray_fromAny.   copy=0 means don't copy unless you have to.

> 2) Is asarray is basically array with copy=0?
Yes.
>
> 3) Is asanyarray basically array with copy=0 and subok=1?
Yes.
>
> 4) Is there some sort of precedence table for conversions? To me it 
> looks like the most deeply nested lists are converted to arrays first, 
> numeric if they contain all numeric types, object otherwise. I assume 
> the algorithm then ascends up through the hierarchy like traversing a 
> binary tree in postorder?
I'm not sure I understand what you mean.  The discover-depth and 
discover-dimensions algorithm figures out what the shape should be and 
then recursive PySequence_GetItem and PySequence_SetItem is used to copy 
the information over to the ndarray from the nested sequence.

>
> 5) All nesting must be to the same depth and the deepest nested items 
> must have the same length.
Yes, there are routines discover_depth and discover_dimensions that are 
the actual algorithm used.  These are adapted from Numeric.
>
> 6) How is the difference between lists and "lists" determined, i.e.,
>
> In [3]: array([list([1,2,3]),list([1,2])], dtype = object)
> Out[3]: array([[1, 2, 3], [1, 2]], dtype=object)
>
> In [8]: array([array([1,2,3]),array([1,2])], dtype = object)
> Out[8]: array([[1 2 3], [1 2]], dtype=object)
>
>
> In [9]: array([1,2,3],[1,2]], dtype = object)
> ------------------------------------------------------------
>    File "<ipython console>", line 1
>      array([1,2,3],[1,2]], dtype = object)
>                         ^
> SyntaxError: invalid syntax

I think this is just due to a missing [ in In [9].   There is no 
semantic difference between
list([1,2,3]) and [1,2,3] (NumPy will see those things as exactly the 
same).
>
> Is the difference that list(...) and array(...) are passed as 
> functions (lazy evaluation), but a list is just a list?
There is nothing like "lazy evaluation" going on.  array([1,2,3]) is 
evaluated returning an object and array([1,2]) is evaluated returning an 
object and then the two are put into another object array.  Equivalent code

a = array([1,2,3])
b = array([1,2])
c = array([a,b],dtype=object)


Thanks for all your help with documentation.  It is very-much appreciated.

-Travis





More information about the Numpy-discussion mailing list