I have a numpy array subclass, and I’d like to be able to concatenate them.
import numpy as np
class BreakfastArray(np.ndarray):
def __new__(cls, n=1):
dtypes=[("waffles", int), ("eggs", int)]
obj = np.zeros(n, dtype=dtypes).view(cls)
return obj
b1 = BreakfastArray(n=1)
b2 = BreakfastArray(n=2)
con_b1b2 = np.concatenate([b1, b2])
print(b1.__class__, con_b1b2.__class__)
this outputs <class '__main__.BreakfastArray'> <class 'numpy.ndarray'>
, but I’d like the concatenated array to also be a BreakfastArray
class. It looks like I probably need to add a __array_finalize__
method, but I can’t figure out the right way to do it.
0
Create con_bib2
as a new BreakfastArray
, then use out=con_bib2
when concatenating to store into it.
con_bib2 = BreakfastArray(n=3)
np.concatenate([b1, b2], out=con_bib2)