[Numpy-discussion] field names on numpy arrays

Pierre GM pgmdevlist@gmail....
Wed Jun 3 10:46:08 CDT 2009


On Jun 3, 2009, at 11:06 AM, D2Hitman wrote:

>
> Hi,
>
> I would like to have an object/class that acts like array of floats  
> such as:
> a_array = numpy.array([[0.,1.,2.,3.,4.],[1.,2.,3.,4.,5.]])
> but i would like to be able to slice this array by some header  
> dictionary:
> header_dict = {'a':0,'b':1,'c':2,'d':3,'e':4}
> such that i could use a_array['a'],
> which would get slice=header_dict['a'],
> slices a_array[:,slice]
> and return it.
>
> I understand record arrays such as:
> ...
> however i seem to lose simple operations such as multiplication  
> (a_array*2)
> or powers (a_array**2).

Indeed. Structured arrays don't support many operations (because  
they're fairly general. For example, one field could be a string, how  
do you define * for strings ?)


> Is there something that does this? Or how would i go about creating an
> object/class that inherits all properties from numpy.array, but adds  
> in a
> header to select columns?

Quick shot (not tested):
Subclass ndarray, define headdict as an attribute, overwrite  
__getitem__ w/ your own, where you test on the input:
* if the input item is a string then take the corresponding column  
from your headdict
* if the input item is anything else, use ndarray.__getitem__

Info on subclassing:
http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

Note that you may want to put some fences here and there to prevent  
reshaping (else your headdict will become useless).

As a side-note, I think this subject has been talked about on this  
mailing list (or scipy's). Sorry, no particular, but you may want to  
check the archives.





More information about the Numpy-discussion mailing list