I’d like to create a subclass of numpy.recarray that automatically resizes when data is added to a row outside of its current length.
The code below does most of what I want.
class autorecarray(numpy.recarray):
def __init__(self,*args,**kwargs):
self._increment = 1
numpy.recarray.__init__(self,args,kwargs)
def __setitem__(self,ind,y):
try:
numpy.recarray.__setitem__(self,ind,y)
except IndexError:
self.resize((self.__len__()+self._increment,),refcheck=False)
self.__setitem__(ind,y)
It works fine for this use case:
a = utils.autorecarray((1,),formats=['i4','i4'])
a[1] = (1,2) # len(a) will now be 2
However, this usage will raise an IndexError on numpy.core.records.recarray __getitem__ method:
a[2]['f1'] = 3
My initial attempt was to also override the __getitem__ method in my subclass, but this code does not work.
def __getitem__(self,ind):
try:
numpy.recarray.__getitem__(self,ind)
except IndexError:
self.resize((self.__len__() + self._increment,),refcheck=False)
self.__getitem__(ind)
It does automatically expand the array, but now every item in the array is None and cannot be changed.
Can anyone tell me what I’m doing wrong?
First of all you’re missing the asterisks in the
numpy.recarray.__init__call:And second, you’re missing
returnstatements in the__getitem__: