[SciPy-user] Masked arrays with dtype=object: elements get wrapped in 0D arrays

Jouni K. Seppänen jks at iki.fi
Tue Jan 9 10:15:48 CST 2007


For a numpy array A with dtype=object, the elements A[i] are the
objects stored in the array, not wrapped in zero-dimensional arrays.
For a numpy.core.ma array A with dtype=object, this seems to be
initially true, but no longer for elements that have been assigned to.
The following script demonstrates this:

------------------------------------------------------------------------
import numpy

for constructor in numpy.array, numpy.core.ma.array:
    print '===', constructor, '==='
    a = constructor([1,2,3,4.0,'foo'],dtype=object)
    print "Expecting: int, int, int, float, str"
    print [type(a[i]) for i in range(len(a))]
    a[0] = 0; a[2] = 0; a[4] = 0
    print "Expecting: int, int, int, float, int"
    print [type(a[i]) for i in range(len(a))]
------------------------------------------------------------------------

My output (with numpy.__version__ == '1.0.1.dev3435') is

------------------------------------------------------------------------
=== <built-in function array> ===
Expecting: int, int, int, float, str
[<type 'int'>, <type 'int'>, <type 'int'>, <type 'float'>, <type 'str'>]
Expecting: int, int, int, float, int
[<type 'int'>, <type 'int'>, <type 'int'>, <type 'float'>, <type 'int'>]
=== <class 'numpy.core.ma.MaskedArray'> ===
Expecting: int, int, int, float, str
[<type 'int'>, <type 'int'>, <type 'int'>, <type 'float'>, <type 'str'>]
Expecting: int, int, int, float, int
[<type 'numpy.ndarray'>, <type 'int'>, <type 'numpy.ndarray'>, <type 'float'>, <type 'numpy.ndarray'>]
------------------------------------------------------------------------

Is this a bug, or am I understanding something wrong?

-- 
Jouni K. Seppänen
http://www.iki.fi/jks



More information about the SciPy-user mailing list