I have a string array (pretty huge ~1Billion elements) and another array with integers. I want to slice the individual string based on its corresponding element in the second array. I have got it worked in python but when I try to translate it to numba, I get error (I believe numba is prohibiting string slicing for e.g. a = ‘abc’; a[1:] #string slice. Any way to solve this problem?
I am trying to jit the following python_clipper function:
arr1 = ['abc', 'def', 'xyz']
arr2 = [1,2,3]
def python_clipper(arr1,arr2):
for i in range(len(arr1)):
arr1[i] = arr1[i][arr2[i]:]
return arr1
print(python_clipper(arr1,arr2)) # ['bc', 'f', '']
I am ok with arr1 and arr2 being np.array as well.
Do you have an example of what you’d like to do that works in plain python but not when numba jitted?
Do you have access to an NVIDIA GPU? If you aren’t able to find a Numba-only solution, it may be that cuDF would enable your use case, as it includes Numba extensions for string handling: Overview of User Defined Functions with cuDF — cudf 23.02.00 documentation
@nelson2005 I have edited the post to include the example.
@gmarkall I have the nvidia GPU but am unfamiliar with cuDF. I will go through the docs to see how I can implement it.
Something like this works for me
import numba
import numpy as np
arr1 = np.asarray(['abc', 'def', 'xyz'])
arr2 = np.asarray([1, 2, 3])
@numba.njit()
def python_clipper(arr1, arr2):
for i in range(len(arr1)):
arr1[i] = str(arr1[i])[arr2[i]:]
return arr1
print(python_clipper(arr1,arr2)) # ['bc', 'f', '']
How about with @cuda.jit()?