I am trying to implement a subclass of a numpy recarray (recsub) and assign instances of it to an ndarray of dtype 'object' (ndarr). It works well, but i have a problem when the subclassed recarray is instantiated with an empty array. This is the code for the subclassed recarry:
class recsub(numpy.recarray):
"""subclassed recarray"""
def __new__(cls, *args, **kwargs):
obj = numpy.recarray.__new__(cls, *args, **kwargs)
return obj
def __init__(self, *arg, **kwargs):
self.x = -1
def new_method(self):
print 'new_method() : fooooooooooooo'
I create the ndarray as :
ndarr = numpy.ndarray(5, 'object')
now if i create two instances of recsub :
ndarr[0] = recsub(2, [('a','f8')])
ndarr[1] = recsub((), [('a','f8')])
Now here is the weird stuff that is happening. The output of :
print type(ndarr[0])
print type(ndarr[1])
is:
>>> <class '__main__.recsub'>
>>> <class 'numpy.core.records.record'>
so i can not access ndarr[1].x
This used to work in numpy 1.7, but not anymore in numpy 1.8! So it seems something is missing upon instantiating the recarray with a shape () as opposed to (n)
any suggestion is welcome,
tnx in advance,