question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

datetime64 arrays don't support buffer protocol

See original GitHub issue
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

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:5
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
abalkincommented, Mar 18, 2016

I suspect the problem is at Objects/memoryobject.c:731,

   (void)PyBuffer_FillInfo(&mbuf->master, NULL, mem, size, readonly,
                            PyBUF_FULL_RO);

where the return value of PyBuffer_FillInfo is ignored. I’ll submit an upstream issue.

0reactions
eric-wiesercommented, 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.

def _as_buffer(arr):
    try:
        return memoryview(arr)
    except ValueError:
        dt = arr.dtype
        new_dt = np.dtype([
            ('__numpy_value', (np.void, dt.itemsize)),
            ('__numpy_dtype', [
                (str(dt), [])
            ])
        ])
        return memoryview(arr.view(new_dt))

def _from_buffer(b):
    arr = np.asarray(b)
    if np.issubdtype(arr.dtype, np.void) and arr.dtype.names == ('__numpy_value', '__numpy_dtype'):
        new_dtype_name, = arr.dtype['__numpy_dtype'].names
        return arr['__numpy_value'].view(new_dtype_name)
    else:
        return arr

Which works as

>>> a = np.array(['now', 'now'], np.datetime64) + [0, 1]
>>> buffer_a = _as_bufferable(a)
>>> buffer_a.format
'T{8x:__numpy_value:T{T{}:datetime64[s]:}:__numpy_dtype:}'

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

datetime64 arrays don't support buffer protocol - Bountysource
datetime64 arrays don't support buffer protocol. ... In [4] m = memoryview(np.array(['2001-01-01'], dtype='M8[D]')) ...
Read more >
How to create of Numpy array of datetime64 objects using C ...
I found a good solution. Here is my function that creates numpy array from a C buffer. PyObject* create_datetime_array(int index, ...
Read more >
pybind/Lobby - Gitter
So, yeah, somewhere, the combination of buffer protocol and numpy arrays does not support datetime64, I think. But as you said, you can...
Read more >
The array interface protocol — NumPy v1.24 Manual
Cython provides a way to write code that supports the buffer protocol with Python versions older than 2.6 because it has a backward-compatible...
Read more >
Release notes — numcodecs 0.10.0a4.dev5+dirty ...
Fix a flatten array error for ZFPY, ZFPY codec is supported on Python 3.9 and 3.10 on ... Handle (new) buffer protocol conforming...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found