I have a subclassed numpy array – a “spectroscopic axis” with metadata such as units.
I can create a copy of any instance using arr.copy or a view using, e.g., arr2=arr[2:5].
Is there any way to create a new instance of one of these arrays with a different shape but keeping the same metadata / data type?
A simple solution would be to define a
.reshapemethod in your subclass. This.reshapemethod should call the__array_finalize__method that would be in charge of copying the metadata from your object.For example, the
MaskedArraysubclass uses a_update_fromprivate method that copies some metadata (_fill_value,_optinfo…) from one object to another or sets defaults for these metadata if they are not yet available. The_update_methodis called within__array_finalize__. You could follow the sources as an example.