Copies with MaskedArray?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Copies with MaskedArray?

Andrew Bell
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
[hidden email]

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion