Skip to content

BUG: can't register cast functions between datetime64 and user dtype #21365

@alexhsamuel

Description

@alexhsamuel

Describe the issue:

I'd like to register (unsafe) cast functions between datetime64 and a custom dtype. Per the docs, I use PyArray_RegisterCastFunc to register these. Essentially this logic:

// ...
PyArray_RegisterDataType(descr);
// ...
npy_datetime = PyArray_DescrFromType(NPY_DATETIME);
PyArray_RegisterCastFunc(npy_datetime, descr->type_num, cast_from_datetime);
PyArray_RegisterCastFunc(descr, NPY_DATETIME, cast_to_datetime);

These return no error state, and in fact PyArray_GetCastFunc returns the functions I set.

My actual code is here and happy to provide additional details about what I'm trying to do.

Until 1.20, this worked as expected (Time is my custom dtype):

>>> np.can_cast(np.dtype("datetime64[ns]"), Time, casting="unsafe")
True
>>> np.can_cast(Time, np.dtype("datetime64[ns]"), casting="unsafe")
True
>>> a
array([ora.Time(2022, 4, 20, 4, 3, 8.82545807, UTC)], dtype=Time)
>>> a.astype("datetime64[ns]")
array(['2022-04-20T04:03:08.825458080'], dtype='datetime64[ns]')

Starting in 1.21,

>>> np.can_cast(np.dtype("datetime64[ns]"), Time, casting="unsafe")
False
>>> np.can_cast(Time, np.dtype("datetime64[ns]"), casting="unsafe")
False
>>> a.astype("datetime64[ns]")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot cast array data from dtype(Time) to dtype('<M8[ns]') according to the rule 'unsafe'

I assume this has something to do with NEP 42, though I don't understand it well enough to understand whether there is now a different mechanism available for adding conversions to/from datetime64.

Reproduce the code example:

import numpy
import ora

numpy.array([ora.now()]).astype("datetime64[ns]")

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot cast array data from dtype(Time) to dtype('<M8[ns]') according to the rule 'unsafe'

NumPy/Python version information:

>>> print(numpy.__version__, sys.version)
1.21.0 3.8.13 (default, Apr 19 2022, 21:32:23)
[GCC 11.2.0]

Also tested in current main 55aacc7.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions