# Creating numpy Datetime64

**URL:** <https://numba.discourse.group/t/creating-numpy-datetime64/2237>\
**Category:** Support: How do I do ...?\
**Created:** [October 23, 2023, 12:28pm UTC](https://numba.discourse.group/t/creating-numpy-datetime64/2237 "2023-10-23T12:28:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![QuantFreedom1022](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/quantfreedom1022/32/827_2.png) [@QuantFreedom1022](https://numba.discourse.group/u/QuantFreedom1022)\
**Post date:** [October 23, 2023, 12:28pm UTC](https://numba.discourse.group/t/creating-numpy-datetime64/2237/1 "2023-10-23T12:28:01Z")

</div>

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](https://github.com/numba/numba/issues/6555)

```python
import numba

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

log_debug()

```

```auto

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

```python
import numba as nb
import numpy as np

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

print(x())

```

---

<div class="post-metadata">

**Author:** ![Oyibo](https://avatars.discourse-cdn.com/v4/letter/o/4af34b/32.png) [@Oyibo](https://numba.discourse.group/u/Oyibo)\
**Post date:** [October 23, 2023, 5:55pm UTC](https://numba.discourse.group/t/creating-numpy-datetime64/2237/2 "2023-10-23T17:55:52Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![QuantFreedom1022](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/quantfreedom1022/32/827_2.png) [@QuantFreedom1022](https://numba.discourse.group/u/QuantFreedom1022)\
**Post date:** [October 23, 2023, 6:19pm UTC](https://numba.discourse.group/t/creating-numpy-datetime64/2237/3 "2023-10-23T18:19:27Z")

</div>

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

```python
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))

```

```auto
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')
    ^

```

---

<div class="post-metadata">

**Author:** ![QuantFreedom1022](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/quantfreedom1022/32/827_2.png) [@QuantFreedom1022](https://numba.discourse.group/u/QuantFreedom1022)\
**Post date:** [October 23, 2023, 6:21pm UTC](https://numba.discourse.group/t/creating-numpy-datetime64/2237/4 "2023-10-23T18:21:19Z")

</div>

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](https://numba.discourse.group/t/sending-a-jitclass-to-another-jitclass/2235)

---

<div class="post-metadata">

**Author:** ![Oyibo](https://avatars.discourse-cdn.com/v4/letter/o/4af34b/32.png) [@Oyibo](https://numba.discourse.group/u/Oyibo)\
**Post date:** [October 23, 2023, 6:27pm UTC](https://numba.discourse.group/t/creating-numpy-datetime64/2237/5 "2023-10-23T18:27:00Z")

</div>

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?

> <https://github.com/numba/numba/issues/4495>
>
> Hi, currently jitclasses allow methods to be declared with keyword arguments, bu…t unlike njit functions, it is not possible to call these methods with keyword arguments. The problem ocurs with and without default values
> \`\`\`python
> import numba
> @numba.jitclass(spec=\[\])
> class norm\_gen():
>     
> def \_\_init\_\_(self):
> pass
>     
> def foo(self, loc=0, size=1):
> return size
>     
> def bar(self, loc, size):
> return size
>     
> norm = norm\_gen()
> \#all these fail
> norm.foo(loc=0, size=10)
> norm.foo(size=10)
> norm.bar(loc=0, size=10)
> \#this works
> norm.foo(0, 10)
> \`\`\`

---

<div class="post-metadata">

**Author:** ![QuantFreedom1022](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/quantfreedom1022/32/827_2.png) [@QuantFreedom1022](https://numba.discourse.group/u/QuantFreedom1022)\
**Post date:** [October 23, 2023, 7:54pm UTC](https://numba.discourse.group/t/creating-numpy-datetime64/2237/6 "2023-10-23T19:54:53Z")

</div>

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
