[Numpy-discussion] 0-dimensional object arrays
Ed Schofield
schofield at ftw.at
Tue Jan 24 06:16:02 CST 2006
Hi all,
I've been attempting to use an object array to group together a
collection of different-length arrays or lists, and I'm getting
unexpected results. Here's an example:
>>> import numpy
>>> a = numpy.array([[1,2],[1,2,3]],object)
I expected here that 'a' would be an array of dimension (2,), holding
two Python lists of integers. Was I wrong to expect this? It seems
instead that 'a' is a 0-dimensional array holding one object:
>>> a
array([[1, 2], [1, 2, 3]], dtype=object)
>>> a.shape
()
>>> type(a)
<type 'numpy.ndarray'>
>>> a[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: 0-d arrays can't be indexed.
This behaviour seems less useful. Would you agree this is a bug in the
array constructor?
I also can't explain this behaviour:
>>> [] + a
array([], dtype=object)
>>> a + []
array([], dtype=object)
I guess I would have expected these commands to be equivalent
array(a.view()+[]).
These results seem more sensible:
>>> a.transpose()
[[1, 2], [1, 2, 3]]
>>> a*2
[[1, 2], [1, 2, 3], [1, 2], [1, 2, 3]]
although these last two commands return Python lists, without the 0-d
array wrapper, which isn't consistent with other 0-d arrays.
On the whole, 0-d object arrays seem quite strange beasts. Could
someone please enlighten me on why they deserve to exist? ;) They seem
inconsistent with the new simplified interface to object array
elements. Could we get rid of them entirely?!
-- Ed
More information about the Numpy-discussion
mailing list