Creating numpy Datetime64

i am trying to create a datetime inside a njit function and getting errors still Cannot create datetime inside njit · Issue #6555 · numba/numba · GitHub

import numba


@numba.njit(cache=True)
def log_debug():
    timestamp = np.array(1697884200000, dtype="datetime64[ms]")
    print(str(timestamp).replace("T", " "))
    pass


log_debug()

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function array>) found for signature:
 
 >>> array(Literal[int](1697884200000), dtype=Literal[str](datetime64[ms]))
 
There are 2 candidate implementations:
      - Of which 1 did not match due to:
      Overload in function 'impl_np_array': File: numba\np\arrayobj.py: Line 5384.
        With argument(s): '(int64, dtype=unicode_type)':
       Rejected as the implementation raised a specific error:
         TypingError: If np.array dtype is a string it must be a string constant.
  raised from e:\Coding\venvs\qfree\lib\site-packages\numba\np\arrayobj.py:4340
      - Of which 1 did not match due to:
      Overload in function 'impl_np_array': File: numba\np\arrayobj.py: Line 5384.
        With argument(s): '(Literal[int](1697884200000), dtype=Literal[str](datetime64[ms]))':
       Rejected as the implementation raised a specific error:
         NumbaNotImplementedError: Failed in nopython mode pipeline (step: native lowering)
       Cannot cast Literal[int](1697884200000) to datetime64[ms]: i64 %"arg.object"
       During: lowering "$8call_function.3 = call $2load_global.0(object, dtype, func=$2load_global.0, args=[Var(object, arrayobj.py:5394), Var(dtype, arrayobj.py:5394)], kws=(), vararg=None, varkwarg=None, target=None)" at e:\Coding\venvs\qfree\lib\site-packages\numba\np\arrayobj.py (5395)
  raised from e:\Coding\venvs\qfree\lib\site-packages\numba\core\base.py:701

During: resolving callee type: Function(<built-in function array>)
During: typing of call at C:\Users\User\AppData\Local\Temp\ipykernel_36068\4261607707.py (6)


File "C:\Users\User\AppData\Local\Temp\ipykernel_36068\4261607707.py", line 6:
def log_debug():
    timestamp = np.array(1697884200000, dtype="datetime64[ms]")

but also what is weird is that you can create an empty array as a datetime64 and that works … but if you try to create an array with a number as a datetime it doesn’t work

import numba as nb
import numpy as np

@njit()
def x():
  return np.empty(1, dtype="datetime64[ms]")

print(x())

Hey @QuantFreedom1022 ,

Datetime creation seems to be a problem. It is not pretty but you can indirectly create a datetime by multiplying the unix timestamp with the timedelta unit and add the result to the epoch start.

import numpy as np
import numba as nb
epoch = np.datetime64('1970-01-01', 'ms')
td_unit = np.timedelta64(1, 'ms')

@nb.njit
def create_date(time_since_epoch):
    return epoch + time_since_epoch * td_unit

print(create_date(1697884200000))
# 2023-10-21T10:30:00.000

Thank you but the problem is it fails if you try to create that inside of a numba function

import numpy as np
import numba as nb

@nb.njit()
def create_date(time_since_epoch):
    epoch = np.datetime64('1970-01-01', 'ms')
    td_unit = np.timedelta64(1, 'ms')
    return epoch + time_since_epoch * td_unit

print(create_date(1697884200000))
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(datetime64[]) found for signature:
 
 >>> <unknown function>(Literal[str](1970-01-01), Literal[str](ms))
 
There are 2 candidate implementations:
 - Of which 2 did not match due to:
 Overload in function 'make_callable_template.<locals>.generic': File: numba\core\typing\templates.py: Line 174.
   With argument(s): '(unicode_type, unicode_type)':
  Rejected as the implementation raised a specific error:
    TypingError: too many positional arguments
  raised from e:\Coding\venvs\qfree\lib\site-packages\numba\core\typing\templates.py:412

During: resolving callee type: class(datetime64[])
During: typing of call at C:\Users\User\AppData\Local\Temp\ipykernel_19292\890831128.py (6)


File "C:\Users\User\AppData\Local\Temp\ipykernel_19292\890831128.py", line 6:
def create_date(time_since_epoch):
    epoch = np.datetime64('1970-01-01', 'ms')
    ^

i know this is off topic but do you happen to know what could be causing these two errors Sending a jitclass to another jitclass

Hey @QuantFreedom1022 ,
It only works if you put “epoch” and “td_unit” outside of the function as global variables. Sorry, I have no other idea. Hopefully somebody else can help you out.

There is a bug related to keyword arguments in jitclasses that sounds similar to your problem.
Have you read Issue 4495 already?

yeah the first error i can live with … but it is the second one that is killing me … do you happen to know what could possibly cause that