I was wondering if there was an easy way to create a class to handle both integer and keyword indexing of a numpy array of numbers.
The end goal is to have a numpy array that I can also index using the names of each variable. For example, if I have the lists
import numpy as np
a = [0,1,2,3,4]
names = ['name0','name1','name2','name3','name4']
A = np.array(a)
I would like to be able to get the values of A easily with a call of (for example) A['name1'], yet have the array retain all of the functionality of a numpy array.
Thanks!
Peter
Edit:
Thanks so much for the help, I'll try to be more clear on the intended use! I have an existing set of code which uses a numpy array to store and apply a vector of variables. My vector has around 30 entries.
When I want to see the value of a particular variable, or when I want to make a change to one of them, I have to remember which entry corresponds to which variable (the order or number of entries doesn't necessarily change once the array is created). Right now I use a dictionary to keep track. For example, I have a numpy array 'VarVector' with with 30 values. "vmax" is entry 15, with a value of 0.432. I'll then have a concurrent dictionary with 30 keys 'VarDict', such that VarDict[entry] = index. This way I can find the value of vmax by chaining the calls
VarVector[VarDict["vmax"]]
which would return 0.432
I was wondering if there would be a good way of simply combining these two structures, such that both VarVector[15] (for compatibility) and VarVector["vmax"] (for convenience to me) would point to the same number.
Thanks! Peter
__getitem__
of a numpy array is already quite slow. You're not going to significantly slow things down by adding this to it. However, this is a fairly common use case and has already been done a couple of times (pandas
andlarry
). Have a look at this comparison: scipy.org/StatisticalDataStructures Having "labeled axes" or "labeled items" is a nice thing to have in some cases. – Joe Kington Jan 18 at 0:06