pymc-devs / pymc-devs/pytensor

ENH: support datetime64[ns] dtype

Open
#439 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Before
index = array(['2017-06-02T00:00:00.000000000', '2017-06-03T00:00:00.000000000',
       '2017-06-04T00:00:00.000000000', ...,
       '2021-06-28T00:00:00.000000000', '2021-06-29T00:00:00.000000000',
       '2021-06-30T00:00:00.000000000'], dtype='datetime64[ns]')

pt.as_tensor(index)

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File .venv/lib/python3.10/site-packages/pytensor/tensor/type.py:287, in TensorType.dtype_specs(self)
    286 try:
--> 287     return self.dtype_specs_map[self.dtype]
    288 except KeyError:

KeyError: 'datetime64[ns]'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 pt.as_tensor(index)

File .venv/lib/python3.10/site-packages/pytensor/tensor/__init__.py:49, in as_tensor_variable(x, name, ndim, **kwargs)
     17 def as_tensor_variable(
     18     x: TensorLike, name: Optional[str] = None, ndim: Optional[int] = None, **kwargs
     19 ) -> "TensorVariable":
     20     """Convert `x` into an equivalent `TensorVariable`.
     21 
     22     This function can be used to turn ndarrays, numbers, `ScalarType` instances,
   (...)
     47 
     48     """
---> 49     return _as_tensor_variable(x, name, ndim, **kwargs)

File /usr/lib/python3.10/functools.py:889, in singledispatch.<locals>.wrapper(*args, **kw)
    885 if not args:
    886     raise TypeError(f'{funcname} requires at least '
    887                     '1 positional argument')
--> 889 return dispatch(args[0].__class__)(*args, **kw)

File .venv/lib/python3.10/site-packages/pytensor/tensor/basic.py:176, in _as_tensor_numbers(x, name, ndim, dtype, **kwargs)
    171 @_as_tensor_variable.register(np.bool_)
    172 @_as_tensor_variable.register(np.number)
    173 @_as_tensor_variable.register(Number)
    174 @_as_tensor_variable.register(np.ndarray)
    175 def _as_tensor_numbers(x, name, ndim, dtype=None, **kwargs):
--> 176     return constant(x, name=name, ndim=ndim, dtype=dtype)

File venv/lib/python3.10/site-packages/pytensor/tensor/basic.py:229, in constant(x, name, ndim, dtype)
    223             raise ValueError(
    224                 f"ndarray could not be cast to constant with {int(ndim)} dimensions"
    225             )
    227     assert x_.ndim == ndim
--> 229 ttype = TensorType(dtype=x_.dtype, shape=x_.shape)
    231 return TensorConstant(ttype, x_, name=name)

File .venv/lib/python3.10/site-packages/pytensor/tensor/type.py:116, in TensorType.__init__(self, dtype, shape, name, broadcastable)
    113         return s
    115 self.shape = tuple(parse_bcast_and_shape(s) for s in shape)
--> 116 self.dtype_specs()  # error checking is done there
    117 self.name = name
    118 self.numpy_dtype = np.dtype(self.dtype)

File .venv/lib/python3.10/site-packages/pytensor/tensor/type.py:289, in TensorType.dtype_specs(self)
    287     return self.dtype_specs_map[self.dtype]
    288 except KeyError:
--> 289     raise TypeError(
    290         f"Unsupported dtype for {self.__class__.__name__}: {self.dtype}"
    291     )

TypeError: Unsupported dtype for TensorType: datetime64[ns]
After
No error
Context for the issue:

Would be nice to have native support for datetimes in pytensor

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the dtype handling shown in pytensor/tensor/type.py and the conversion path in pytensor/tensor/init.py and pytensor/tensor/basic.py. Trace how pt.as_tensor receives the datetime64[ns] array, then verify that the example completes without the unsupported-dtype error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.