[SciPy-dev] examples

Travis Oliphant oliphant at ee.byu.edu
Thu Dec 8 16:58:51 CST 2005


Christopher Hanley wrote:

> Hi Travis,
>
> I've been doing a little playing with your records module today.  I 
> was wondering if you could give me a couple examples of it's usage.  
> In particular, how would you accomplish the following done in numarray?
>
> r=array([(1,11,'a'),(2,22,'b'),(3,33,'c'),(4,44,'d'),(5,55,'ex'),(6,66,'f'),(7,77,'g')],formats='u1,f4,a1') 
>
>
> Thank you for your time and help,


There is no fromarrays function yet, so there is no records.array 
function yet.  

It should be a straightforward matter to create one (you shouldn't have 
to distinguish between chararrays and other arrays though).

In this case say x is the list of tuples.

r = ndrecarray(len(x), formats= 'u1,f4,S1')

for ind, val in enumerate(x):
    for k in range(len(val)):
        r[ind]['f%d' %(k+1)] = val[k]

should do it (get recent SVN, there was a bug in setitem that prevented 
this from working before).

Then (on my system):

 >>> r
ndrecarray([[1, 11.0, 'a'], [2, 22.0, 'b'], [3, 33.0, 'c'], [4, 44.0, 'd'],
       [5, 55.0, 'e'], [6, 66.0, 'f'], [7, 77.0, 'g']], dtype=record6)

 >>> r.f1
ndrecarray([1, 2, 3, 4, 5, 6, 7], dtype=uint8)
 >>> r.f2
ndrecarray([ 11.,  22.,  33.,  44.,  55.,  66.,  77.], dtype=float32)
 >>> r.f3
ndrecarray([a, b, c, d, e, f, g], dtype=string1)

Obviously a fromarray function is needed to make this process easier.  
This function would need to be a bit more general to handle 
multidimensional cases and arbitrary field names. 

The format string does not allow an easy way to right nested records, 
but the general ndrecarray allows it.  The infrastructure for making it 
easy to deal with nested records is there but the user-convenience may 
not be.

I would love your help on the fromarray, fromstring, and friends functions.


-Travis


-Travis




More information about the Scipy-dev mailing list