jakirkham / cybuffer

A Python 2/3 compatible buffer in Cython
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

datetime/timedelta not supported #10

Open jakirkham opened 5 years ago

jakirkham commented 5 years ago

Description

Currently datetime and timedelta objects are not supported. This is essentially a consequence of the fact that memoryviews themselves lack support for these types. That said, a reasonable fix for this kind of problem involves casting the data to another type first (e.g. int64) and then building a memoryview using that casting.

What I Did

In [1]: import numpy as np                                                      

In [2]: from cybuffer import cybuffer                                           

In [3]: a = np.datetime64('2015-07-04 12:59:59.50', 'ns')[None]                 

In [4]: b = cybuffer(a)                                                         
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-d5e2b2b7f8b3> in <module>
----> 1 b = cybuffer(a)

src/cybuffer.pyx in cybuffer.cybuffer.__cinit__()

ValueError: cannot include dtype 'M' in a buffer
jakirkham commented 5 years ago

FWIW this can happen without cybuffer.

In [1]: import numpy as np                                                      

In [2]: a = np.datetime64('2015-07-04 12:59:59.50', 'ns')[None]                 

In [3]: memoryview(a)                                                           
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-dd900d27ccee> in <module>
----> 1 memoryview(a)

ValueError: cannot include dtype 'M' in a buffer
jakirkham commented 5 years ago

Looking a bit more closely, it appears this is actually coming from NumPy. We could certainly circumvent this by checking for a NumPy array and casting it if needed. Though it's unclear whether doing this implicitly is better than just having users do this explicitly.