[Numpy-discussion] subclassing ndarray and recarray

Pierre GM pgmdevlist at gmail.com
Thu May 10 20:10:45 EDT 2007


Bernhard,
Looks like you have to modify your __getitem__ and __getslice__ methods. The 
following seems to work in simple cases.

The numpy.array in front of numpy.ndarray.__getxxx__ is to transform 
the 'numpy.void

    def __getitem__(self, key):
        if isinstance(key, arraydict) and key.dtype == bool:
            # The key is a condition
            return self.select(self.keys(key))
        elif isinstance(key, str):
            # The key is a string, therefore a field
            return numpy.ndarray.__getitem__(self, key)
        else:
            # The key is a sequence or a scalar
            obj = numpy.array(numpy.ndarray.__getitem__(self, 
key)).view(type(self))
            key = numpy.array(key, copy=False, ndmin=1)
            # _mapping won't like dealing w/ negative keys: add n to them
            key[key<0] += len(self)
            obj.__make_mapping(key)
            return obj

    def __getslice__(self,i,j):
        obj = numpy.array(numpy.ndarray.__getslice__(self, 
i,j)).view(type(self))
        obj.__make_mapping(range(i,j+1))
        return obj



More information about the NumPy-Discussion mailing list