[Numpy-discussion] array with named columns (or record arrays with homogenous types)

Tony S Yu tonyyu at MIT.EDU
Mon Sep 22 19:25:16 EDT 2008


Is there any way to name the columns of an array so that they can be  
called with a string, but still used like an ordinary array? What I  
have in mind is very similar to a record array, but with homogenous  
types. Having different types implies that certain operations (e.g.,  
mean, sum) don't make sense, in general.

For example, I want to do something like:
#~~~~~~~
 >>> import numpy as np

 >>> color = np.array([(1, 2, 3), (4, 5, 6)],
...                  dtype=[('r', '<f4'), ('g', '<f4'), ('b', '<f4')])
# get the average (grayscale) of the first row
 >>> color[0].mean()
TypeError: cannot perform reduce with flexible type
#~~~~~~~
BTW, I actually want add the fields (columns) for all records (rows),  
not just each row individually, but `color.mean(axis=1)` doesn't make  
sense because `color` is actually a 1D array of records.

Thanks,
-Tony



More information about the NumPy-Discussion mailing list