This information seems to be not easy to find, my reference is pretty old (2011), but not much seems to have changed since then.
Numpy's bool-array uses a 8bit-value for False/True (this is not obvious per se - C++'s std::vector<bool>
uses for example 1 bit per value) with 0
-meaning False
and 1
-meaning True
. You can use cast=True
for an unit8
-array in order to use it as a bool
-array, for example:
%%cython
import numpy as np
cimport numpy as np
def to_bool_array(lst):
cdef np.ndarray[np.uint8_t, ndim = 1, cast=True] res
res=np.array(lst, dtype=bool)
return res
And now:
>>> to_bool_array([True,False,True,False])
array([ True, False, True, False], dtype=bool)
Setting cast=True
gives some slack to Cython's type-checking, so the numpy-arrays with the same element-size (for example uint8
, int8
and bool
) can be reinterpreted. This however would not work if element-sizes were different: for example np.int8
(1byte) and np.int16
(2bytes).