[SciPy-dev] Nested arrays

Robert Kern robert.kern at gmail.com
Tue Jan 10 14:18:53 CST 2006


Sasha wrote:
> 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 think we should reconsider the decision to return scalar array objects when
indexing into object arrays. The concerns for indexing into numeric arrays are
much different than for object arrays. There are no type casting issues with
object arrays. People use object arrays expecting to be able to access the
objects themselves, not some proxy. I'm not sure that the genericity benefits
(e.g. always being able to get the .shape attribute) one gets with scalar arrays
carry over very well to the use cases of object arrays. Object arrays have
always really been a collection of special cases, so I'm not terribly concerned
about giving them exceptional behavior.

-- 
Robert Kern
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Scipy-dev mailing list