[Numpy-discussion] Behavior of numpy.copy with sub-classes

Jonathan Helmus jjhelmus at gmail.com
Mon Oct 19 22:23:18 EDT 2015


In GitHub issue #3474, a number of us have started a conversation on how 
NumPy's copy function should behave when passed an instance which is a 
sub-class of the array class.  Specifically, the issue began by noting 
that when a MaskedArray is passed to np.copy, the sub-class is not 
passed through but rather a ndarray is returned.

I suggested adding a "subok" parameter which controls how sub-classes 
are handled and others suggested having the function call a copy method 
on duck arrays.  The "subok" parameter is implemented in PR #6509 as an 
example. Both of these options would change the API of numpy.copy and 
possibly break backwards compatibility.  Do others have an opinion of 
how np.copy should handle sub-classes?

For a concrete example of this behavior and possible changes, what type 
should copy_x be in the following snippet:

import numpy as np
x = np.ma.array([1,2,3])
copy_x = np.copy(x)


Cheers,

     - Jonathan Helmus



More information about the NumPy-Discussion mailing list