[Numpy-discussion] Subclassing ndarray with concatenate
Sebastian Berg
sebastian@sipsolutions....
Tue Jan 22 06:44:33 CST 2013
Hey,
On Tue, 2013-01-22 at 10:21 +0100, Todd wrote:
> I am trying to create a subclass of ndarray that has additional
> attributes. These attributes are maintained with most numpy functions
> if __array_finalize__ is used.
>
You can cover a bit more if you also implement `__array_wrap__`, though
unless you want to do something fancy, that just replaces the
`__array_finalize__` for the most part. But some (very few) functions
currently call `__array_wrap__` explicitly.
> The main exception I have found is concatenate (and hstack/vstack,
> which just wrap concatenate). In this case, __array_finalize__ is
> passed an array that has already been stripped of the additional
> attributes, and I don't see a way to recover this information.
>
There are quite a few functions that simply do not preserve subclasses
(though I think more could/should call `__array_wrap__` probably, even
if the documentation may say that it is about ufuncs, there are some
example of this already).
`np.concatenate` is one of these. It always returns a base array. In any
case it gets a bit difficult if you have multiple input arrays (which
may not matter for you).
> In my particular case at least, there are clear ways to handle corner
> cases (like being passed a class that lacks these attributes), so in
> principle there no problem handling concatenate in a general way,
> assuming I can get access to the attributes.
>
>
> So is there any way to subclass ndarray in such a way that concatenate
> can be handled properly?
>
Quite simply, no. If you compare masked arrays, they also provide their
own concatenate for this reason.
I hope that helps a bit...
Regards,
Sebastian
> I have been looking extensively online, but have not been able to find
> a clear answer on how to do this, or if there even is a way.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list