[SciPy-dev] Nested arrays

Sasha ndarray at mac.com
Tue Jan 10 13:17:13 EST 2006


In Numeric nested arrays could be indexed using multiple subscripts:

>>> from Numeric import *
>>> x = empty(2,'O')
>>> x[0] = array([1,2])
>>> x[1] = array([1,2,3])
>>> x
array([[1 2] , [1 2 3] ],'O')
>>> x[1][2]
3

This feature is lost in NumPy:

>>> x = empty(2, object)
>>> x[0] = array([1,2])
>>> x[1] = array([1,2,3])
>>> x[1][2]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsubscriptable object

This is quite surprising because x[1] prints as an array
>>> x[1]
array([1, 2, 3])

The problem is that x[1] is in fact a numpy scalar object holding an array:
>>> x[1].size
1
>>> type(x[1])
<type 'object_arrtype'>

I can work around this problem as follows:
>>> x[1].item()[2]
3

but this is quite ugly and requires a lot of changes for the legacy code.

I propose to add ndarray to a list of possible dtypes and not to wrap
ndarrays in object_ instances. There are many other possible solutions
to this problem, but I think that nested
arrays a common enough to deserve special treatment.

-- sasha




More information about the SciPy-Dev mailing list