|
1 | 1 | from pandas.core.exchange.dataframe_protocol import Buffer, DlpackDeviceType, DtypeKind
|
2 | 2 | import numpy as np
|
3 | 3 | from typing import Tuple
|
4 |
| -import ctypes |
5 |
| - |
6 |
| - |
7 |
| -_NP_DTYPES = { |
8 |
| - DtypeKind.INT: {8: np.int8, 16: np.int16, 32: np.int32, 64: np.int64}, |
9 |
| - DtypeKind.UINT: {8: np.uint8, 16: np.uint16, 32: np.uint32, 64: np.uint64}, |
10 |
| - DtypeKind.FLOAT: {32: np.float32, 64: np.float64}, |
11 |
| - DtypeKind.BOOL: {8: bool}, |
12 |
| -} |
13 | 4 |
|
14 | 5 |
|
15 | 6 | class PandasBuffer(Buffer):
|
@@ -74,25 +65,3 @@ def __repr__(self) -> str:
|
74 | 65 | )
|
75 | 66 | + ")"
|
76 | 67 | )
|
77 |
| - |
78 |
| - |
79 |
| -def buffer_to_ndarray(_buffer: Buffer, _dtype) -> np.ndarray: |
80 |
| - # Handle the dtype |
81 |
| - kind = _dtype[0] |
82 |
| - bitwidth = _dtype[1] |
83 |
| - if kind not in _NP_DTYPES: |
84 |
| - raise RuntimeError(f"Unsupported data type: {kind}") |
85 |
| - |
86 |
| - column_dtype = _NP_DTYPES[kind][bitwidth] |
87 |
| - |
88 |
| - # No DLPack yet, so need to construct a new ndarray from the data pointer |
89 |
| - # and size in the buffer plus the dtype on the column |
90 |
| - ctypes_type = np.ctypeslib.as_ctypes_type(column_dtype) |
91 |
| - data_pointer = ctypes.cast(_buffer.ptr, ctypes.POINTER(ctypes_type)) |
92 |
| - |
93 |
| - # NOTE: `x` does not own its memory, so the caller of this function must |
94 |
| - # either make a copy or hold on to a reference of the column or |
95 |
| - # buffer! (not done yet, this is pretty awful ...) |
96 |
| - x = np.ctypeslib.as_array(data_pointer, shape=(_buffer.bufsize // (bitwidth // 8),)) |
97 |
| - |
98 |
| - return x |
0 commit comments