[Numpy-discussion] atleast_3d() strips subclass info

Benjamin Root ben.root@ou....
Tue Jul 27 12:07:33 CDT 2010

I have filed ticket #1560: http://projects.scipy.org/numpy/ticket/1560

 The .atleast_3d() function can not be used with arrays that are subclassed
from ndarray(). Because the function uses .asarray() instead of
.asanyarray(), subclass info such as masks for masked arrays are stripped
from the results. See the following example:

>>> a = numpy.ma.masked_array([0.0, 1.2, 3.5], mask=[False, True, False])
>>> b = numpy.atleast_3d(a)
>>> b
array([[[ 0. ],
        [ 1.2],
        [ 3.5]]])

>>> a
masked_array(data = [0.0 -- 3.5],
             mask = [False  True False],
       fill_value = 1e+20)


Note that the .atleast_1d() and .atleast_2d() functions are safe for
subclassed arrays. I believe that simply replacing .asarray() with
.asanyarray() should fix the issue.
This ticket has a proposed patch that replaces .asarray() with

Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100727/0920a6fe/attachment.html 

More information about the NumPy-Discussion mailing list