You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
datetime64 arrays don't support buffer protocol #4983
In [4] m = memoryview(np.array(['2001-01-01'], dtype='M8[D]'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-033a8bae908a> in <module>()
----> 1 m = memoryview(np.array(['2001-01-01'], dtype='M8[D]'))
ValueError: cannot include dtype 'M' in a buffer
Reacted by Eric Zhang, Mikhail Veygman and Ivan Smirnov
@abalkin: what do you even want to have happen, given that memoryview
itself does not support datetime64? I guess we could export it as an array
of int64s, but that's already possible with an explicit cast
(arr.view(np.int64)), and probably the explicit form is better anyway...
On 22 Aug 2014 22:01, "Charles Harris" [email protected] wrote:
Ah, this is going to be a tough one. I don't think memoryview supports
datetime.
—
Reply to this email directly or view it on GitHub #4983 (comment).
I would like to be able to access datetime64 data at the C level without introducing dependency on numpy API. AFAIK, memoryview does not check what the PEP 3118 buffer interface puts in format. It only needs this for its tolist() method which is not implemented many types anyways. For example, in Python 3.4.2:
>>> a = numpy.array([None])
>>> m = memoryview(a)
>>> m.tolist()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError: memoryview: format O not supported
On the other hand, format is happily reported:
>>> m.format
'O'
My proposal is that datetime64 arrays should behave as int64, but report their format as 'M[..]'.
@abalkin The upstream behaviour has changed with issue 23571 in Python 3.5, and now it triggers a SystemError. See also #7270. Is it still an upstream bug?
>>> from numpy import *
>>> memoryview(zeros(10, dtype=[("A", "m8[s]")]))
ValueError: cannot include dtype 'm' in a buffer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: <class 'memoryview'> returned a result with an error set
I believe this shows that there is a bug in Python itself. Apparently, in this situation memoryview constructor sets a ValueError, but returns a non-NULL result.
An idea I had about this (which I might have put elsewhere) was to embed numpy type data in the existing buffer spec by exploiting the fact that T{} uses no storage space.
Activity
charris commentedon Aug 22, 2014
Ah, this is going to be a tough one. I don't think memoryview supports datetime.
njsmith commentedon Aug 22, 2014
@abalkin: what do you even want to have happen, given that memoryview
itself does not support datetime64? I guess we could export it as an array
of int64s, but that's already possible with an explicit cast
(arr.view(np.int64)), and probably the explicit form is better anyway...
On 22 Aug 2014 22:01, "Charles Harris" [email protected] wrote:
abalkin commentedon Oct 20, 2014
I would like to be able to access datetime64 data at the C level without introducing dependency on numpy API. AFAIK, memoryview does not check what the PEP 3118 buffer interface puts in format. It only needs this for its tolist() method which is not implemented many types anyways. For example, in Python 3.4.2:
On the other hand, format is happily reported:
My proposal is that datetime64 arrays should behave as int64, but report their format as 'M[..]'.
gerritholl commentedon Feb 24, 2015
Why does
memoryview(zeros(10, dtype="m8[s]"))fail as noted in this bug report, but the following not throw any error?It does report an error if I assign it to a variable. See http://stackoverflow.com/q/28708411/974555 .
abalkin commentedon Mar 15, 2015
@gerritholl - this is something that should be reported upstream.
gerritholl commentedon Feb 17, 2016
@abalkin The upstream behaviour has changed with issue 23571 in Python 3.5, and now it triggers a SystemError. See also #7270. Is it still an upstream bug?
abalkin commentedon Mar 18, 2016
With Python 3.5.1, I get
I believe this shows that there is a bug in Python itself. Apparently, in this situation memoryview constructor sets a ValueError, but returns a non-NULL result.
abalkin commentedon Mar 18, 2016
I suspect the problem is at Objects/memoryobject.c:731,
where the return value of PyBuffer_FillInfo is ignored. I'll submit an upstream issue.
TDaltonC commentedon Jul 6, 2016
I'm also seeing this error (python 3.5.1 Numpy 1.11.0), but it looks like people might have solved a very similar problem?
joblib/joblib#188
jzwinck commentedon Apr 24, 2017
The accepted solution on Stack Overflow for how to take specific columns of a recarray fails because of this. See http://stackoverflow.com/a/21819324/4323 for the full details, or try:
The expected result is:
But it fails with
ValueError: cannot include dtype 'M' in a buffer.jakirkham commentedon Nov 24, 2018
So it looks like PR ( #11694 ) made it so that one could do this...
However running this as well results in an exception...
Would it be possible to extend the same scalar fix to
datetime64andtimedelta64arrays generally?eric-wieser commentedon Jan 10, 2020
An idea I had about this (which I might have put elsewhere) was to embed numpy type data in the existing buffer spec by exploiting the fact that
T{}uses no storage space.Which works as
A scarier approach might be to use
pickle.dumps(arr.dtype).encode('hex')as the field name, to ensure that absolutely any user dtype can be used.