2

I am trying to use a NumPy array of strings as an input for a cython function. However, I can't seem to figure out how to actually use a numPy array of strings with cython.

Here is an example of the array I am trying to use as an input for my function:

np.array(['foo', 'bar', 'baz', 'foo', 'bar'])

Attempt 1:

#cython: language_level=3
import numpy as np
cimport numpy as np
cimport cython

def test_print(np.ndarray[str, ndim=1] unique_product_uids_np):
    cdef str key
    for key in unique_product_uids_np:
        print(key)

The above code compiles but when you try and use the function, you get the following error

arr = np.array(['foo', 'bar', 'baz', 'foo', 'bar'])
does_this_work.test_print(arr)

Does not understand character buffer dtype format string ('w')

Attempt 2:

#cython: language_level=3
import numpy as np
cimport numpy as np
cimport cython

def test_print(np.ndarray[np.str_, ndim=1] unique_product_uids_np):
    cdef np.str_ key
    for key in unique_product_uids_np:
        print(key)






Error when compiling:
    def test_print(np.ndarray[np.str_, ndim=1] unique_product_uids_np):
                                    ^
    does_this_work.pyx:6:33: Invalid type.

I did the exact same test for unicode and got the same results as above. where unicode compiled but threw the Does not understand character buffer dtype format string ('w') and np.unicode_ didn't compile and threw the Invalid type error while compiling.

Here is what my setup.py file looks

from distutils.core import setup
from Cython.Build import cythonize
import numpy

setup(ext_modules = cythonize('does_this_work.pyx', annotate=True),
      compiler_directives={'language_level' : "3"},
      include_dirs=[numpy.get_include()])

and here is the command I am using to compile:

python setup.py build_ext --inplace

I am using: Python version 3.10, Cython version 0.29.33, NumPy version 1.24.2

Note: there are a few other post about this but none of the post really resolve the problem here and here

neuron
  • 1,949
  • 1
  • 15
  • 30

0 Answers0