# Change datetime64 type within a jitted func

**URL:** <https://numba.discourse.group/t/change-datetime64-type-within-a-jitted-func/2090>\
**Category:** Support: How do I do ...?\
**Created:** [August 24, 2023, 10:32am UTC](https://numba.discourse.group/t/change-datetime64-type-within-a-jitted-func/2090 "2023-08-24T10:32:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joueswant](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/joueswant/32/829_2.png) [@joueswant](https://numba.discourse.group/u/joueswant)\
**Post date:** [August 24, 2023, 10:32am UTC](https://numba.discourse.group/t/change-datetime64-type-within-a-jitted-func/2090/1 "2023-08-24T10:32:28Z")

</div>

Hi amazing team!

Assume we have an array `t` which is a np.datetime64. I want to operate with him inside an njitted function (change the type of the datetime and do some math ops. afterwards). Is that possible?

So far I’ve got this:

```auto
from numba.types import NPDatetime

@jit(nopython=True)
def foo(dt64_arr):
    return dt64_arr.astype(NPDatetime("M"))
foo(t)

```

Yet the error I got is the following:

> Blockquote  
> TypingError: Failed in nopython mode pipeline (step: nopython frontend)  
> Invalid use of \<class ‘numba.core.types.scalars.NPDatetime’\> with parameters (Literalstr)  
> No type info available for \<class ‘numba.core.types.scalars.NPDatetime’\> as a callable.  
> During: resolving callee type: typeref[\<class ‘numba.core.types.scalars.NPDatetime’\>]  
> During: typing of call at /tmp/ipykernel\_961918/589338060.py (5)  
> File “…/…/…/…/tmp/ipykernel\_961918/589338060.py”, line 5:

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [August 24, 2023, 1:02pm UTC](https://numba.discourse.group/t/change-datetime64-type-within-a-jitted-func/2090/2 "2023-08-24T13:02:30Z")

</div>

Can you supply a complete example that people can test?

---

<div class="post-metadata">

**Author:** ![joueswant](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/joueswant/32/829_2.png) [@joueswant](https://numba.discourse.group/u/joueswant)\
**Post date:** [August 24, 2023, 1:46pm UTC](https://numba.discourse.group/t/change-datetime64-type-within-a-jitted-func/2090/3 "2023-08-24T13:46:38Z")

</div>

Thanks a lot for your prompt reply Nelson, sure thing.

I expect numba to behave like this

```auto
import numpy as np
t = np.arange('2005-02', '2005-06', dtype='datetime64[D]')

def foo_np(dt64_arr):
    return dt64_arr.astype("datetime64[M]")
foo_np(t)

```

To do so, i used the function described above

```auto
from numba.types import NPDatetime
t = np.arange('2005-02', '2005-06', dtype='datetime64[D]')

@jit(nopython=True)
def foo_nb(dt64_arr):
    return dt64_arr.astype(NPDatetime("M"))
foo_nb(t)

```

But it returns an error, described in the first post.  
My final goal is to do something like this:

```auto
  def foo_final(self):
 
      return self.astype('datetime64[M]').astype(np.int32) % 12 + 1

```

But using an independent numba function

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [August 25, 2023, 5:58pm UTC](https://numba.discourse.group/t/change-datetime64-type-within-a-jitted-func/2090/4 "2023-08-25T17:58:21Z")

</div>

Okay, I’ll make some comments. I don’t know to use numpy.astype() to convert generic parameterized types like datetime64 from ‘D’ to ‘M’.

That being said, a couple of observations:

1. if you can do the units-conversion in non-jitted code, or an objmode block, you can use the pointer to the numpy data to do the cast to int32 and other math directly in a jitted function
2. if you want to/are willing to implement the D-to-M conversion yourself, getting the integer values and doing that logic in the numba block seems possible. There’s a numpy reference [here](https://github.com/numpy/numpy/blob/4cbced22a8306d7214a4e18d12e651c034143922/numpy/core/src/multiarray/datetime.c#l432)

Either/both of these options are workarounds that may be more work than you want to do for your problem. Perhaps someone else on the board will have better options 🙂
