Repository: numba/numba

Issue:
`searchsorted` with dtype `datetime64` stopped working in `njit` mode going from 0.58.1 to 0.59
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I have tried using the latest released version of Numba (most recent is
  visible in the release notes
  (https://numba.readthedocs.io/en/stable/release-notes-overview.html).
- [x] I have included a self contained code sample to reproduce the problem.
  i.e. it's possible to run as 'python bug.py'.

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->

Using `numpy.searchsorted()` with dtype `datetime64` does not work in `njit` mode in version 0.59. This works fine in version 0.58.1.

I guess this is related to the recent changes to the function. #9189 

Reproducible sample:
```
import numpy as np
from numba import njit

@njit
def foo(a, v):
    return np.searchsorted(a, v)

arr = np.asarray(
    ['2022-03-09 06:45:00', '2022-03-09 06:50:00', '2022-03-09 06:55:00'],
    dtype=np.dtype('datetime64[ns]')
)
print(foo(arr, arr[-1]))
```

Output:
```
---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
Cell In[37], line 12
      6     return np.searchsorted(a, v)
      8 arr = np.asarray(
      9     ['2022-03-09 06:45:00', '2022-03-09 06:50:00', '2022-03-09 06:55:00'],
     10     dtype=np.dtype('datetime64[ns]')
     11 )
---> 12 print(foo(arr, arr[-1]))

File ~/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
    464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
    465                f"by the following argument(s):\n{args_str}\n")
    466         e.patch_message(msg)
--> 468     error_rewrite(e, 'typing')
    469 except errors.UnsupportedError as e:
    470     # Something unsupported is present in the user code, add help info
    471     error_rewrite(e, 'unsupported_error')

File ~/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/dispatcher.py:409, in _DispatcherBase._compile_for_args.<locals>.error_rewrite(e, issue_type)
    407     raise e
    408 else:
--> 409     raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function searchsorted at 0xffff645c9ee0>) found for signature:
 
 >>> searchsorted(array(datetime64[ns], 1d, C), datetime64[ns])
 
There are 2 candidate implementations:
      - Of which 1 did not match due to:
      Overload in function 'searchsorted': File: numba/np/arraymath.py: Line 3805.
        With argument(s): '(array(datetime64[ns], 1d, C), datetime64[ns])':
       Rejected as the implementation raised a specific error:
         TypingError: Failed in nopython mode pipeline (step: nopython frontend)
       No implementation of function Function(<function _searchsorted.<locals>.impl at 0xfffd3a10df80>) found for signature:
        
        >>> impl(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))
        
       There are 2 candidate implementations:
         - Of which 2 did not match due to:
         Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
           With argument(s): '(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))':
          Rejected as the implementation raised a specific error:
            TypingError: Failed in nopython mode pipeline (step: nopython frontend)
          No implementation of function Function(<function _less_than at 0xfffd3e842d40>) found for signature:
           
           >>> _less_than(datetime64[ns], datetime64[ns])
           
          There are 2 candidate implementations:
                - Of which 2 did not match due to:
                Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
                  With argument(s): '(datetime64[ns], datetime64[ns])':
                 Rejected as the implementation raised a specific error:
                   TypingError: Failed in nopython mode pipeline (step: nopython frontend)
                 No implementation of function Function(<built-in function isinstance>) found for signature:
                  
                  >>> isinstance(datetime64[ns], Function(<class 'complex'>))
                  
                 There are 2 candidate implementations:
                       - Of which 2 did not match due to:
                       Overload in function 'ol_isinstance': File: numba/cpython/builtins.py: Line 748.
                         With argument(s): '(datetime64[ns], Function(<class 'complex'>))':
                        Rejected as the implementation raised a specific error:
                          NumbaTypeError: isinstance() does not support variables of type "datetime64[ns]".
                   raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/cpython/builtins.py:778
                 
                 During: resolving callee type: Function(<built-in function isinstance>)
                 During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3729)
                 
                 
                 File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3729:
                 def _less_than(a, b):
                     if isinstance(a, complex) or isinstance(b, complex):
                     ^
          
            raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
          
          During: resolving callee type: Function(<function _less_than at 0xfffd3e842d40>)
          During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3753)
          
          
          File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3753:
              def impl(a, v):
                  <source elided>
          
                      if func_1(last_key_val, key_val):
                      ^
       
         raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
       
       During: resolving callee type: Function(<function _searchsorted.<locals>.impl at 0xfffd3a10df80>)
       During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3830)
       
       
       File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3830:
               def impl(a, v, side='left'):
                   return _impl(a, np.array([v]))[0]
                   ^

  raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
      - Of which 1 did not match due to:
      Overload in function 'searchsorted': File: numba/np/arraymath.py: Line 3805.
        With argument(s): '(array(datetime64[ns], 1d, C), datetime64[ns])':
       Rejected as the implementation raised a specific error:
         TypingError: Failed in nopython mode pipeline (step: nopython frontend)
       No implementation of function Function(<function _searchsorted.<locals>.impl at 0xfffd3c92e520>) found for signature:
        
        >>> impl(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))
        
       There are 2 candidate implementations:
         - Of which 2 did not match due to:
         Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
           With argument(s): '(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))':
          Rejected as the implementation raised a specific error:
            TypingError: Failed in nopython mode pipeline (step: nopython frontend)
          No implementation of function Function(<function _less_than at 0xfffd3e842d40>) found for signature:
           
           >>> _less_than(datetime64[ns], datetime64[ns])
           
          There are 2 candidate implementations:
                - Of which 2 did not match due to:
                Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
                  With argument(s): '(datetime64[ns], datetime64[ns])':
                 Rejected as the implementation raised a specific error:
                   TypingError: Failed in nopython mode pipeline (step: nopython frontend)
                 No implementation of function Function(<built-in function isinstance>) found for signature:
                  
                  >>> isinstance(datetime64[ns], Function(<class 'complex'>))
                  
                 There are 2 candidate implementations:
                       - Of which 2 did not match due to:
                       Overload in function 'ol_isinstance': File: numba/cpython/builtins.py: Line 748.
                         With argument(s): '(datetime64[ns], Function(<class 'complex'>))':
                        Rejected as the implementation raised a specific error:
                          NumbaTypeError: isinstance() does not support variables of type "datetime64[ns]".
                   raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/cpython/builtins.py:778
                 
                 During: resolving callee type: Function(<built-in function isinstance>)
                 During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3729)
                 
                 
                 File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3729:
                 def _less_than(a, b):
                     if isinstance(a, complex) or isinstance(b, complex):
                     ^
          
            raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
          
          During: resolving callee type: Function(<function _less_than at 0xfffd3e842d40>)
          During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3753)
          
          
          File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3753:
              def impl(a, v):
                  <source elided>
          
                      if func_1(last_key_val, key_val):
                      ^
       
         raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
       
       During: resolving callee type: Function(<function _searchsorted.<locals>.impl at 0xfffd3c92e520>)
       During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3830)
       
       
       File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3830:
               def impl(a, v, side='left'):
                   return _impl(a, np.array([v]))[0]
                   ^

  raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091

During: resolving callee type: Function(<function searchsorted at 0xffff645c9ee0>)
During: typing of call at /tmp/ipykernel_12047/1166298884.py (6)


File "../../../../../tmp/ipykernel_12047/1166298884.py", line 6:
<source missing, REPL/exec in use?>
```
`searchsorted` with dtype `datetime64` stopped working in `njit` mode going from 0.58.1 to 0.59
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I have tried using the latest released version of Numba (most recent is
  visible in the release notes
  (https://numba.readthedocs.io/en/stable/release-notes-overview.html).
- [x] I have included a self contained code sample to reproduce the problem.
  i.e. it's possible to run as 'python bug.py'.

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->

Using `numpy.searchsorted()` with dtype `datetime64` does not work in `njit` mode in version 0.59. This works fine in version 0.58.1.

I guess this is related to the recent changes to the function. #9189 

Reproducible sample:
```
import numpy as np
from numba import njit

@njit
def foo(a, v):
    return np.searchsorted(a, v)

arr = np.asarray(
    ['2022-03-09 06:45:00', '2022-03-09 06:50:00', '2022-03-09 06:55:00'],
    dtype=np.dtype('datetime64[ns]')
)
print(foo(arr, arr[-1]))
```

Output:
```
---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
Cell In[37], line 12
      6     return np.searchsorted(a, v)
      8 arr = np.asarray(
      9     ['2022-03-09 06:45:00', '2022-03-09 06:50:00', '2022-03-09 06:55:00'],
     10     dtype=np.dtype('datetime64[ns]')
     11 )
---> 12 print(foo(arr, arr[-1]))

File ~/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
    464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
    465                f"by the following argument(s):\n{args_str}\n")
    466         e.patch_message(msg)
--> 468     error_rewrite(e, 'typing')
    469 except errors.UnsupportedError as e:
    470     # Something unsupported is present in the user code, add help info
    471     error_rewrite(e, 'unsupported_error')

File ~/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/dispatcher.py:409, in _DispatcherBase._compile_for_args.<locals>.error_rewrite(e, issue_type)
    407     raise e
    408 else:
--> 409     raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function searchsorted at 0xffff645c9ee0>) found for signature:
 
 >>> searchsorted(array(datetime64[ns], 1d, C), datetime64[ns])
 
There are 2 candidate implementations:
      - Of which 1 did not match due to:
      Overload in function 'searchsorted': File: numba/np/arraymath.py: Line 3805.
        With argument(s): '(array(datetime64[ns], 1d, C), datetime64[ns])':
       Rejected as the implementation raised a specific error:
         TypingError: Failed in nopython mode pipeline (step: nopython frontend)
       No implementation of function Function(<function _searchsorted.<locals>.impl at 0xfffd3a10df80>) found for signature:
        
        >>> impl(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))
        
       There are 2 candidate implementations:
         - Of which 2 did not match due to:
         Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
           With argument(s): '(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))':
          Rejected as the implementation raised a specific error:
            TypingError: Failed in nopython mode pipeline (step: nopython frontend)
          No implementation of function Function(<function _less_than at 0xfffd3e842d40>) found for signature:
           
           >>> _less_than(datetime64[ns], datetime64[ns])
           
          There are 2 candidate implementations:
                - Of which 2 did not match due to:
                Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
                  With argument(s): '(datetime64[ns], datetime64[ns])':
                 Rejected as the implementation raised a specific error:
                   TypingError: Failed in nopython mode pipeline (step: nopython frontend)
                 No implementation of function Function(<built-in function isinstance>) found for signature:
                  
                  >>> isinstance(datetime64[ns], Function(<class 'complex'>))
                  
                 There are 2 candidate implementations:
                       - Of which 2 did not match due to:
                       Overload in function 'ol_isinstance': File: numba/cpython/builtins.py: Line 748.
                         With argument(s): '(datetime64[ns], Function(<class 'complex'>))':
                        Rejected as the implementation raised a specific error:
                          NumbaTypeError: isinstance() does not support variables of type "datetime64[ns]".
                   raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/cpython/builtins.py:778
                 
                 During: resolving callee type: Function(<built-in function isinstance>)
                 During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3729)
                 
                 
                 File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3729:
                 def _less_than(a, b):
                     if isinstance(a, complex) or isinstance(b, complex):
                     ^
          
            raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
          
          During: resolving callee type: Function(<function _less_than at 0xfffd3e842d40>)
          During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3753)
          
          
          File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3753:
              def impl(a, v):
                  <source elided>
          
                      if func_1(last_key_val, key_val):
                      ^
       
         raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
       
       During: resolving callee type: Function(<function _searchsorted.<locals>.impl at 0xfffd3a10df80>)
       During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3830)
       
       
       File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3830:
               def impl(a, v, side='left'):
                   return _impl(a, np.array([v]))[0]
                   ^

  raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
      - Of which 1 did not match due to:
      Overload in function 'searchsorted': File: numba/np/arraymath.py: Line 3805.
        With argument(s): '(array(datetime64[ns], 1d, C), datetime64[ns])':
       Rejected as the implementation raised a specific error:
         TypingError: Failed in nopython mode pipeline (step: nopython frontend)
       No implementation of function Function(<function _searchsorted.<locals>.impl at 0xfffd3c92e520>) found for signature:
        
        >>> impl(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))
        
       There are 2 candidate implementations:
         - Of which 2 did not match due to:
         Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
           With argument(s): '(array(datetime64[ns], 1d, C), array(datetime64[ns], 1d, C))':
          Rejected as the implementation raised a specific error:
            TypingError: Failed in nopython mode pipeline (step: nopython frontend)
          No implementation of function Function(<function _less_than at 0xfffd3e842d40>) found for signature:
           
           >>> _less_than(datetime64[ns], datetime64[ns])
           
          There are 2 candidate implementations:
                - Of which 2 did not match due to:
                Overload in function 'register_jitable.<locals>.wrap.<locals>.ov_wrap': File: numba/core/extending.py: Line 161.
                  With argument(s): '(datetime64[ns], datetime64[ns])':
                 Rejected as the implementation raised a specific error:
                   TypingError: Failed in nopython mode pipeline (step: nopython frontend)
                 No implementation of function Function(<built-in function isinstance>) found for signature:
                  
                  >>> isinstance(datetime64[ns], Function(<class 'complex'>))
                  
                 There are 2 candidate implementations:
                       - Of which 2 did not match due to:
                       Overload in function 'ol_isinstance': File: numba/cpython/builtins.py: Line 748.
                         With argument(s): '(datetime64[ns], Function(<class 'complex'>))':
                        Rejected as the implementation raised a specific error:
                          NumbaTypeError: isinstance() does not support variables of type "datetime64[ns]".
                   raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/cpython/builtins.py:778
                 
                 During: resolving callee type: Function(<built-in function isinstance>)
                 During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3729)
                 
                 
                 File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3729:
                 def _less_than(a, b):
                     if isinstance(a, complex) or isinstance(b, complex):
                     ^
          
            raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
          
          During: resolving callee type: Function(<function _less_than at 0xfffd3e842d40>)
          During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3753)
          
          
          File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3753:
              def impl(a, v):
                  <source elided>
          
                      if func_1(last_key_val, key_val):
                      ^
       
         raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091
       
       During: resolving callee type: Function(<function _searchsorted.<locals>.impl at 0xfffd3c92e520>)
       During: typing of call at /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py (3830)
       
       
       File "../../../.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/np/arraymath.py", line 3830:
               def impl(a, v, side='left'):
                   return _impl(a, np.array([v]))[0]
                   ^

  raised from /home/ec2-user/.cache/pypoetry/virtualenvs/indefatigable-XRADjY79-py3.11/lib/python3.11/site-packages/numba/core/typeinfer.py:1091

During: resolving callee type: Function(<function searchsorted at 0xffff645c9ee0>)
During: typing of call at /tmp/ipykernel_12047/1166298884.py (6)


File "../../../../../tmp/ipykernel_12047/1166298884.py", line 6:
<source missing, REPL/exec in use?>
```


Discussion:
Thanks for the report. This reproduces and is indeed, unfortunately, a regression.
I just hit this issue too. The compiler takes quite a while to throw the error which seems strange to me.
Thanks for the report. This reproduces and is indeed, unfortunately, a regression.
I just hit this issue too. The compiler takes quite a while to throw the error which seems strange to me.

Resolving patch:
diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py
--- a/numba/np/arraymath.py
+++ b/numba/np/arraymath.py
@@ -3735,6 +3735,24 @@ def _less_than(a, b):
     return a < b
 
 
+@register_jitable
+def _less_then_datetime64(a, b):
+    # Original numpy code is at:
+    # https://github.com/numpy/numpy/blob/3dad50936a8dc534a81a545365f69ee9ab162ffe/numpy/_core/src/npysort/npysort_common.h#L334-L346
+    if np.isnat(a):
+        return 0
+
+    if np.isnat(b):
+        return 1
+
+    return a < b
+
+
+@register_jitable
+def _less_then_or_equal_datetime64(a, b):
+    return not _less_then_datetime64(b, a)
+
+
 def _searchsorted(cmp):
     # a facsimile of:
     # https://github.com/numpy/numpy/blob/4f84d719657eb455a35fcdf9e75b83eb1f97024a/numpy/core/src/npysort/binsearch.cpp#L61  # noqa: E501
@@ -3759,8 +3777,13 @@ def impl(a, key_val, min_idx, max_idx):
 def make_searchsorted_implementation(np_dtype, side):
     assert side in VALID_SEARCHSORTED_SIDES
 
-    lt = _less_than
-    le = _less_than_or_equal
+    if np_dtype.char in 'mM':
+        # is datetime
+        lt = _less_then_datetime64
+        le = _less_then_or_equal_datetime64
+    else:
+        lt = _less_than
+        le = _less_than_or_equal
 
     if side == 'left':
         _impl = _searchsorted(lt)


Test patch:
diff --git a/numba/tests/test_npdatetime.py b/numba/tests/test_npdatetime.py
--- a/numba/tests/test_npdatetime.py
+++ b/numba/tests/test_npdatetime.py
@@ -971,21 +971,7 @@ def test_add_td_no_match(self):
     def test_sub_td_no_match(self):
         self._test_add_sub_td_no_match(np.subtract)
 
-    def _test_min_max(self, operation, parallel, method):
-        if method:
-            if operation is np.min:
-                def impl(arr):
-                    return arr.min()
-            else:
-                def impl(arr):
-                    return arr.max()
-        else:
-            def impl(arr):
-                return operation(arr)
-
-        py_func = impl
-        cfunc = njit(parallel=parallel)(impl)
-
+    def _get_testcases(self):
         test_cases = [
             np.array([
                 DT(0, "ns"),
@@ -1067,7 +1053,24 @@ def impl(arr):
                 TD("NaT", "ns"),
             ]),
         ]
+        return test_cases
+
+    def _test_min_max(self, operation, parallel, method):
+        if method:
+            if operation is np.min:
+                def impl(arr):
+                    return arr.min()
+            else:
+                def impl(arr):
+                    return arr.max()
+        else:
+            def impl(arr):
+                return operation(arr)
+
+        py_func = impl
+        cfunc = njit(parallel=parallel)(impl)
 
+        test_cases = self._get_testcases()
         for arr in test_cases:
             py_res = py_func(arr)
             c_res = cfunc(arr)
@@ -1111,6 +1114,31 @@ def test_max_func_parallel(self):
     def test_max_method_parallel(self):
         self._test_min_max(np.max, True, True)
 
+    def test_searchsorted_datetime(self):
+        from .test_np_functions import (
+            searchsorted, searchsorted_left, searchsorted_right,
+        )
+        pyfunc_list = [searchsorted, searchsorted_left, searchsorted_right]
+        cfunc_list = [jit(fn) for fn in pyfunc_list]
+
+        def check(pyfunc, cfunc, a, v):
+            expected = pyfunc(a, v)
+            got = cfunc(a, v)
+            self.assertPreciseEqual(expected, got)
+
+        cases = self._get_testcases()
+        for pyfunc, cfunc in zip(pyfunc_list, cfunc_list):
+            for arr in cases:
+                arr = np.sort(arr)
+                for n in range(1, min(3, arr.size) + 1):
+                    idx = np.random.randint(0, arr.size, n)
+                    vs = arr[idx]
+                    if n == 1:
+                        [v] = vs
+                        check(pyfunc, cfunc, arr, v)
+                    check(pyfunc, cfunc, arr, vs)
+
+
 
 if __name__ == '__main__':
     unittest.main()