Skip to content
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

Cannot convert unicode or char dtype to longer fixed size #6484

Open
2 tasks done
elfjes opened this issue Nov 15, 2020 · 6 comments
Open
2 tasks done

Cannot convert unicode or char dtype to longer fixed size #6484

elfjes opened this issue Nov 15, 2020 · 6 comments

Comments

@elfjes
Copy link

elfjes commented Nov 15, 2020

Reporting a bug

I'm trying to increase the (fixed) length string/unicode dtype of an array. I've tried a number of ways, but none of them work. I think the following two methods should work however, but fail with the error given in the comments:

import numba
import numpy as np

@numba.njit
def assign_in_array(source, destination):
    destination[:] = source

@numba.njit
def using_astype(source, destination):
    converted =source.astype(destination.dtype)
    destination[:] = converted


if __name__ == '__main__':
    assert numba.__version__ == '0.51.2'
    src = np.array(['val'], dtype='S4') # also U4/U8 doesn't work
    dst = np.empty_like(src, dtype='S8')

    assign_in_array(src, dst)
    # numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
    # No implementation of function Function(<built-in function setitem>) found for signature:
    #
    #  >>> setitem(array([unichr x 8], 1d, C), slice<a:b>, array([unichr x 4], 1d, C))
    #
    # There are 16 candidate implementations:
    #   - Of which 16 did not match due to:
    #   Overload of function 'setitem': File: <numerous>: Line N/A.
    #     With argument(s): '(array([unichr x 8], 1d, C), slice<a:b>, array([unichr x 4], 1d, C))':
    #    No match.

    using_astype(src, dst)
    # numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
    # - Resolution failure for literal arguments:
    # astype([char x 8]) not supported on array([char x 4], 1d, C): cannot convert from [char x 4] to [char x 8]
    # - Resolution failure for non-literal arguments:
    # None

Is there any other way that might work?

@esc esc added the needtriage label Nov 16, 2020
@esc
Copy link
Member

esc commented Nov 16, 2020

@elfjes thank you for reporting this, I can confirm the behaviour you describe. I can also confirm that the code will work fine without the Numba decorators.

@esc esc added the numpy label Nov 16, 2020
@elfjes
Copy link
Author

elfjes commented Nov 16, 2020

@esc thanks for your reply. Is there any other way I might be able to do this within numba? Would it perhaps be possible to do something with an @overload decorator?

@stuartarchibald
Copy link
Contributor

I think this is due to a number of valid casts having not been implemented. One path that should work is this:

import numba
import numpy as np

@numba.njit(boundscheck=True)
def assign_in_array(source, destination):
    for i in range(len(destination)):
        destination[i] = str(source[i])

if __name__ == '__main__':
    src = np.array(['val'], dtype='U4')
    dst = np.empty_like(src, dtype='U8')

    assign_in_array(src, dst)

@esc
Copy link
Member

esc commented Nov 16, 2020

@stuartarchibald I tried that, but only with 'S4' and 'S8' and that failed too with:

Traceback (most recent call last):
  File "issue6484.py", line 25, in <module>
    convert(src, dst)
  File "/Users/esc/git/numba/numba/core/dispatcher.py", line 414, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/Users/esc/git/numba/numba/core/dispatcher.py", line 357, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<class 'str'>) found for signature:

 >>> str([char x 4])

There are 6 candidate implementations:
  - Of which 6 did not match due to:
  Overload of function 'str': File: <numerous>: Line N/A.
    With argument(s): '([char x 4])':
   No match.

During: resolving callee type: Function(<class 'str'>)
During: typing of call at issue6484.py (19)


File "issue6484.py", line 19:
def convert(source, destination):
    <source elided>
    for i in range(len(source)):
        destination[i] = str(source[i])

@stuartarchibald
Copy link
Contributor

@esc I anticipate it would fail given that implemented casts do not cover that case.

@esc
Copy link
Member

esc commented Nov 16, 2020

I have re-labelled this as a feature request to add the missing casts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants