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')
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) ]
field()
method:
>>> print r.field(0) ['Smith', 'Johnson', 'Williams', 'Miller'] >>> print r.field('Last_name') ['Smith', 'Johnson', 'Williams', 'Miller']
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) ]
Send comments to the NumArray community.