New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create integer from unicode string with int() #5650
Comments
@bf thanks for asking about this on the Numba issue tracker. Purely from memory, I don't think this is implemented yet and IIRC there was a PR recently to address this. I'll have a closer look now. |
Thank you very much for your quick reply. Luckily, I have found a workaround: By converting to bytesarray first, I can work with the strings as I originally intended. But I am unsure about the performance impact, because converting str to bytesarray in python land involves UTF8 decode, which might be the most expensive operation in this context.
|
So the pull-request I had in mind was actually the other way around: |
@bf I have converted this into a feature request as it doesn't seem to be implemented yet. |
Here is an example implementation that I wrote to illustrate how it would (could) be done: from numba import njit
@njit
def str_to_int(s):
final_index, result = len(s) - 1, 0
for i,v in enumerate(s):
result += (ord(v) - 48) * (10 ** (final_index - i))
return result
print(str_to_int("1"))
print(str_to_int("12"))
print(str_to_int("123"))
print(str_to_int("1234"))
print(str_to_int("12345"))
print(str_to_int("123456"))
print(str_to_int("1234567"))
print(str_to_int("12345678"))
print(str_to_int("123456789"))
print(str_to_int("1234567890")) |
I am trying to convert a unicode string to an integer like this:
and receive the following error:
I have also tried to replace the
int(str_date[0:4])
withint(str_date[0]) * 1000 + int(str_date[1]) * 100 + int(str_date[2]) * 10 + int(str_date[3])
but the same error appears.Is this a real bug or am I using the library wrong? Unfortunately I was unable to find more info on substring handling in the docs. Thank you!
The text was updated successfully, but these errors were encountered: