[Numpy-discussion] Problem with concatenate and object arrays
Charles R Harris
charlesr.harris at gmail.com
Thu Sep 7 17:48:51 CDT 2006
On 9/7/06, Travis Oliphant <oliphant at ee.byu.edu> wrote:
>
> Charles R Harris wrote:
>
> >
> > So is this intentional?
> >
> > In [24]: a = array([[],[],[]], dtype=object)
> >
> > In [25]: a.shape
> > Out[25]: (3, 0)
> >
> > In [26]: a = array([], dtype=object)
> >
> > In [27]: a.shape
> > Out[27]: (0,)
> >
> > One could argue that the first array should have shape (3,)
> >
> Yes, it's intentional because it's the old behavior of Numeric. And it
> follows the rule that object arrays don't do anything special unless the
> old technique of using [] as 'dimension delimiters' breaks down.
>
> >
> > And this doesn't look quite right:
> >
> > In [38]: a = array([[1],[2],[3]], dtype=object)
> >
> > In [39]: a.shape
> > Out[39]: (3, 1)
> >
> > In [40]: a = array([[1],[2,3],[4,5]], dtype=object)
> >
> > In [41]: a.shape
> > Out[41]: (3,)
> >
>
> Again, same reason as before. The first example works fine to construct
> a rectangular array of object arrays of dimension 2. The second only
> does if we limit the number of dimensions to 1.
>
> The rule is that array needs nested lists with the same number of
> dimensions unless you have object arrays. Then, the dimensionality will
> be determined by finding the largest number of dimensions possible for
> consistency of shape.
So there is a 'None' trick:
In [93]: a = array([[[2]], None], dtype=object)
In [94]: a[0]
Out[94]: [[2]]
I wonder if it wouldn't be useful to have a 'depth' keyword. Thus depth=None
is current behavior, but
array([], depth=0)
would produce a zero dimensional array containing an empty list. Although I
notice from playing with dictionaries that a zero dimensional array
containing a dictionary isn't very useful.
array([[],[]], depth=1)
would produce a one dimensional array containing two empty lists, etc. I can
see it is difficult to get something truely general with the current syntax
without a little bit of extra information.
Another question, what property must an object possess to be a container
type argument in array? There are sequence type objects, and array type
objects. Are there more or is everything else treated as an object?
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060907/adf33df0/attachment.html
More information about the Numpy-discussion
mailing list