IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
12.1 Introduction


12.1 Introduction

One of the enhancements of numarray over Numeric is its support for record arrays, i.e. arrays with heterogeneous data types: for example, tabulated data where each field (or column) has the same data type but different fields may not.

Each record array is a RecArray object in the numarray.records module. Most of the time, the easiest way to construct a record array is to use the array() function in the numarray.records module. For example:

>>> import numarray.records as rec
>>> r = rec.array([('Smith', 1234),\
                   ('Johnson', 1001),\
                   ('Williams', 1357),\
                   ('Miller', 2468)], \
                   names='Last_name, phone_number')
In this example, we manually construct a record array by longhand input of the information. This record array has 4 records (or rows) and two fields (or columns). The names of the fields are specified in the names argument. When using this longhand input, the data types (formats) are automatically determined from the data. In this case the first field is a string of 8 characters (since the longest name is 8 characters long) and the second field is an integer.

The record array is just like an array in numarray, except that now each element is a Record. We can do the usual indexing and slicing:

>>> print r[0]
('Smith', 1234)
>>> print r[:2]
RecArray[ 
('Smith', 1234),
('Johnson', 1001)
]
To access the record array's fields, use the field() method:
>>> print r.field(0)
['Smith', 'Johnson', 'Williams', 'Miller']
>>> print r.field('Last_name')
['Smith', 'Johnson', 'Williams', 'Miller']
these examples show that the field method can accept either the numeric index or the field name.

Since each field is simply a numarray of numbers or strings, all functionalities of numarray are available to them. The record array is one single object which allows the user to have either field-wise or row-wise access. The following example:

>>> r.field('phone_number')[1]=9999
>>> print r[:2]
RecArray[ 
('Smith', 1234),
('Johnson', 9999)
]
shows that a change using the field view will cause the corresponding change in the row-wise view without additional copying or computing.

Send comments to the NumArray community.