[Numpy-discussion] PyInt and Numpy's int64 conversion

897 views
Skip to first unread message

xantares 09

unread,
Dec 23, 2011, 4:37:14 AM12/23/11
to numpy-di...@scipy.org
Hi,

I'm using Numpy from the C python api side while tweaking my SWIG interface to work with numpy array types.
I want to convert a numpy array of integers (whose elements are numpy's 'int64')
The problem is that it this int64 type is not compatible with the standard python integer type:
I cannot use PyInt_Check, and PyInt_AsUnsignedLongMask to check and convert from int64: basically PyInt_Check returns false.
I checked the numpy config header and npy_int64 does have a size of 8o, which should be the same as int on my x86_64.
What is the correct way to do that ?
I checked for a Int64_Check function and didn't find any in numpy headers.

Regards,

x.

Wes McKinney

unread,
Dec 23, 2011, 12:31:45 PM12/23/11
to Discussion of Numerical Python
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Di...@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

hello,

I think you'll want to use the C macro PyArray_IsIntegerScalar, e.g.
in pandas I have the following function exposed to my Cython code:

PANDAS_INLINE int
is_integer_object(PyObject* obj) {
return PyArray_IsIntegerScalar(obj);
}

last time I checked that macro detects Python int, long, and all of
the NumPy integer hierarchy (int8, 16, 32, 64). If you ONLY want to
check for int64 I am not 100% sure the best way.

- Wes
_______________________________________________
NumPy-Discussion mailing list
NumPy-Di...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

xantares 09

unread,
Dec 24, 2011, 3:11:42 AM12/24/11
to numpy-di...@scipy.org
Hi,

Thank you for your reply !

That's the thing : I want to check/convert every type of integer, numpy's int64 and also python standard ints.
Is there a way to avoid to use only the python api ? ( and avoid to depend on numpy's PyArray_* functions )

Regards.

x.





Wes McKinney

unread,
Dec 24, 2011, 7:51:06 PM12/24/11
to Discussion of Numerical Python

No. All of the PyTypeObject objects for the NumPy array scalars are
explicitly part of the NumPy C API so you have no choice but to depend
on that (to get the best performance). If you want to ONLY check for
int64 at the C API level, I did a bit of digging and the relevant type
definitions are in

https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/npy_common.h

so you'll want to do:

int is_int64(PyObject* obj){
return PyObject_TypeCheck(obj, &PyInt64ArrType_Type);
}

and that will *only* detect np.int64

- Wes

xantares 09

unread,
Jan 4, 2012, 5:22:39 AM1/4/12
to numpy-di...@scipy.org
Ok many thanks !

One last thing, do you happen to know how to actually convert an np int64 to a C int ?

- x.

Wes McKinney

unread,
Jan 6, 2012, 4:00:00 PM1/6/12
to Discussion of Numerical Python

Not sure off-hand. You'll have to look at the NumPy scalar API in the C code

Travis Oliphant

unread,
Jan 6, 2012, 4:31:45 PM1/6/12
to Discussion of Numerical Python

No. All of the PyTypeObject objects for the NumPy array scalars are
explicitly part of the NumPy C API so you have no choice but to depend
on that (to get the best performance). If you want to ONLY check for
int64 at the C API level, I did a bit of digging and the relevant type
definitions are in


https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/npy_common.h

so you'll want to do:

int is_int64(PyObject* obj){
return PyObject_TypeCheck(obj, &PyInt64ArrType_Type);
}

and that will *only* detect np.int64

- Wes

Ok many thanks !

One last thing, do you happen to know how to actually convert an np int64 to
a C int ?

- x.

Not sure off-hand. You'll have to look at the NumPy scalar API in the C code

What is it you want to do?   Do you want to get the C int out of the np.int64 *Python* object?  

If so, you do: 

npy_int64 val
PyArray_ScalarAsCtype(obj, &val);

If you want to get the C int of a *different* type out of the scalar Python object, you do: 

npy_int32 val
PyArray_Descr * outcode = PyArray_DescrFromType(NPY_INT32);
PyArray_CastScalarToCtype(obj, &val, outcode);
Py_DECREF(outcode);


-Travis

Reply all
Reply to author
Forward
0 new messages