2

I created a pandas dataframe with pd.DataFrame({"A": ["dog", "cat"]}) and sent to a c extension the resulting numpy array. In C, I do:

PyArrayObject * pArray = (PyArrayObject *) pArrayObj;
PyArray_Descr * pDesc = PyArray_DTYPE(pArray);
int kind = pDesc->kind;
npy_intp sz = PyArray_DIM(pArray, 0);
PyObject * p0 = (PyObject *) PyArray_GETPTR1(pArray, 0);

In the above 'kind' contains 'O' (for object array) and sz = 2 (as expected). However p0 does not seem to access the first object:

(gdb) p *p0
$10 = {ob_refcnt = 140737227161872, ob_type = 0x7ffff06e8180}

Notice corrupted refcnt and unresolved ob_type (was expecting PyUnicode_Type).

Any suggestions to correctly obtain a pointer to each element?

I'm using numpy 1.14 on Centos 7, g++ 6.2.1

2 Answers 2

1

I found this question because I had the same problem. Turns out the initial code was almost correct, just some more pointer magic required in the last line:

PyArrayObject * pArray = (PyArrayObject *) pArrayObj;
PyObject * p0 = *(PyObject **) PyArray_GETPTR1(pArray, 0);
Sign up to request clarification or add additional context in comments.

Comments

0

Figured out how to do it:

npy_intp stride = PyArray_STRIDE(pArray, 0);
const char * dataptr = PyArray_BYTES(pArray);
PyObject * p1 = PyArray_GETITEM(pArray, dataptr);
PyObject * p2 = PyArray_GETITEM(pArray, dataptr + stride);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.