[Numpy-discussion] Copies with MaskedArray?

Andrew Bell andrew.bell.ia at gmail.com
Thu Aug 23 17:38:02 EDT 2018


Hi,

I'm creating a structured numpy array in C, wrapping it with a MaskedArray
and passing this to a Python script:

    ...
    PyObject *arr = PyArray_NewFromDescr(&PyArray_Type, descr, 1,
        &longCount, &pointSize, data, NPY_ARRAY_WRITEABLE, nullptr);

    // Now wrap the created array in a MaskedArray.
    PyObject *module = PyImport_ImportModule("numpy.ma");
    PyObject *dict = PyModule_GetDict(module);
    PyObject *keys = PyDict_Keys(dict);
    PyObject *o = PyDict_GetItemString(dict, "MaskedArray");

    PyObject *args = PyTuple_New(1);
    PyTuple_SetItem(args, 0, arr);

    m_directArray = PyObject_CallObject(o, args);

This works fine and the data is seen in python as it was in C.  What I'd
like to happen is to have changes made to the array in Python be reflected
in the memory that was passed in.  This seems to work in some cases.  Given
the dtype:

[('X', '<f8'), ('Y', '<f8'), ('Z', '<f8'), ('OffsetTime', '<u4')]

I can do something like:

X = 10

in Python and appropriate data is set in the C-allocated memory.  However,
this fails:

X *= 10

in that it seems to make a copy of the array IF the mask of the values
aren't all set to 0 in the source.

Is there something I can do to get operations on the MaskedArray to operate
on the data in place as would occur with a raw ndarray?

Thanks,

-- 
Andrew Bell
andrew.bell.ia at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180823/3e9ae70c/attachment.html>


More information about the NumPy-Discussion mailing list