I am trying to create a numpy array of subclassed numpy arrays. Unfortunately, when I create my new array of subclasses, numpy automatically upcasts the elements of my array to numpy.ndarray.
The code below shows what I am trying to do. dummy_class inherits from numpy.ndarray and contains some extra functionality(which is not important for the problem at hand). I create two new arrays using the dummy_class constructor and want to put each of these subclassed arrays in a new numpy_ndarray. When the problematic array gets initialized, the type of the subclassed arrays gets automatically upcast from dummy_class to numpy.ndarray. Some code to reproduce the problem can be found below
import numpy
class dummy_class(numpy.ndarray):
def __new__(cls, data, some_attribute):
obj = numpy.asarray(data).view(cls)
obj.attribute = some_attribute
return obj
array_1 = dummy_class([1,2,3,4], "first dummy")
print type(array_1)
# <class '__main__.dummy_class'>
array_2 = dummy_class([1,2,3,4], "second dummy")
print type(array_2)
# <class '__main__.dummy_class'>
the_problem = numpy.array([array_1, array_2])
print type(the_problem)
# <type 'numpy.ndarray'>
print type(the_problem[0])
# <type 'numpy.ndarray'>
print type(the_problem[1])
# <type 'numpy.ndarray'>
dtype=yourtypeto the ndarrsytheproblem.the_problemhas shape(2,2,3), and is a perfectly valid,efficient numpy array by itself. The other discussion about what numpy tries to achieve or what numpy is built for is opinionated.