[Matrix-SIG] NumPy arrays with object typecode

Oliver Gathmann gathmann@scar.utoronto.ca
Tue, 2 Feb 1999 09:32:11 -0500 (EST)


Hello,

I am working on something like an S-Plus frame that would allow efficient
access to data of different types (integers, floats, and strings)
organized in colums of an ordinary 2d-array. My idea is to use NumPy
arrays of typecode 'O'; I know this sacrifices a lot of the performance of
genuine numerical arrays, but they still offer their nifty indexing and
slicing capabilities.

Strangely, NumPy (LLNL v.9) does only seem to allow assigning to slices
along the second axis, not along the first:

Python 1.5.2b1 (#2, Jan  5 1999, 14:21:49)  [GCC 2.7.2.3] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from Numeric import *
>>> objArray = zeros((3,2),'O')
>>> objArray
array([[0 , 0 ],
       [0 , 0 ],
       [0 , 0 ]],'O')
>>> intArray = reshape(arange(6),(3,2))
>>> intArray
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> objArray[1,:] = intArray[1,:]
>>> objArray
array([[0 , 0 ],
       [2 , 3 ],
       [0 , 0 ]],'O')
>>> objArray[:,1] = intArray[:,1]
Segmentation fault (core dumped)

Explicitly converting the integer slice into an object slice doesn't help,
either. What's going on here?

I also find this counter-intuitive: 

>>> objArrayFromInits = array(['1','12','123'],'O')
>>> objArrayFromInits
array([[1 , 1 , 1 ],
       [12 , 12 , 12 ],
       [123 , 123 , 123 ]],'O')

Is there any efficient way to have the array constructor treat the
individual strings as objects and not as 1d arrays?

Thanks,

Oliver

F. Oliver Gathmann (gathmann@scar.utoronto.ca)
Surface and Groundwater Ecology Research Group      
University of Toronto
phone: (416) - 287 7420 ; fax: (416) - 287 7423     
web: http://www.scar.utoronto.ca/~gathmann