Skip to content

Commit d36b8c6

Browse files
committed
Use constants from spec enums, beautify a bit
Signed-off-by: Vasily Litvinov <vasilij.n.litvinov@intel.com>
1 parent 4f1d213 commit d36b8c6

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

pandas/api/exchange/implementation.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33

44
from typing import Tuple, Any
55

6-
from .dataframe_protocol import Buffer, Column, DataFrame as DataFrameXchg, DtypeKind, DlpackDeviceType
6+
from .dataframe_protocol import Buffer, Column, ColumnNullType, DataFrame as DataFrameXchg, DtypeKind, DlpackDeviceType
77

88
import pandas as pd
99
import numpy as np
1010

11+
_NP_DTYPES = {
12+
DtypeKind.INT: {8: np.int8, 16: np.int16, 32: np.int32, 64: np.int64},
13+
DtypeKind.UINT: {8: np.uint8, 16: np.uint16, 32: np.uint32, 64: np.uint64},
14+
DtypeKind.FLOAT: {32: np.float32, 64: np.float64},
15+
DtypeKind.BOOL: {8: bool},
16+
}
1117

1218
def from_dataframe(df : DataFrameXchg,
1319
allow_copy : bool = True) -> pd.DataFrame:
@@ -65,10 +71,7 @@ def convert_column_to_ndarray(col : Column) -> Tuple[np.ndarray, Buffer]:
6571
"""
6672
Convert an int, uint, float or bool column to a numpy array.
6773
"""
68-
if col.offset != 0:
69-
raise NotImplementedError("column.offset > 0 not handled yet")
70-
71-
if col.describe_null[0] not in (0, 1):
74+
if col.describe_null[0] not in (ColumnNullType.NON_NULLABLE, ColumnNullType.USE_NAN):
7275
raise NotImplementedError("Null values represented as masks or "
7376
"sentinel values not handled yet")
7477

@@ -80,14 +83,10 @@ def buffer_to_ndarray(_buffer : Buffer, _dtype) -> np.ndarray:
8083
# Handle the dtype
8184
kind = _dtype[0]
8285
bitwidth = _dtype[1]
83-
if _dtype[0] not in (DtypeKind.INT, DtypeKind.UINT, DtypeKind.FLOAT, DtypeKind.BOOL):
84-
raise RuntimeError("Not a boolean, integer or floating-point dtype")
86+
if kind not in _NP_DTYPES:
87+
raise RuntimeError(f"Unsupported data type: {kind}")
8588

86-
_ints = {8: np.int8, 16: np.int16, 32: np.int32, 64: np.int64}
87-
_uints = {8: np.uint8, 16: np.uint16, 32: np.uint32, 64: np.uint64}
88-
_floats = {32: np.float32, 64: np.float64}
89-
_np_dtypes = {0: _ints, 1: _uints, 2: _floats, 20: {8: bool}}
90-
column_dtype = _np_dtypes[kind][bitwidth]
89+
column_dtype = _NP_DTYPES[kind][bitwidth]
9190

9291
# No DLPack yet, so need to construct a new ndarray from the data pointer
9392
# and size in the buffer plus the dtype on the column
@@ -124,10 +123,10 @@ def convert_categorical_column(col : Column) -> Tuple[pd.Series, Buffer]:
124123
cat = pd.Categorical(values, categories=categories, ordered=ordered)
125124
series = pd.Series(cat)
126125
null_kind = col.describe_null[0]
127-
if null_kind == 2: # sentinel value
126+
if null_kind == ColumnNullType.USE_SENTINEL: # sentinel value
128127
sentinel = col.describe_null[1]
129128
series[codes == sentinel] = np.nan
130-
else:
129+
elif null_kind != ColumnNullType.NON_NULLABLE:
131130
raise NotImplementedError("Only categorical columns with sentinel "
132131
"value supported at the moment")
133132

@@ -164,16 +163,16 @@ def convert_string_column(col : Column) -> Tuple[np.ndarray, dict]:
164163
str_list = []
165164
for i in range(obuf.size-1):
166165
# Check for missing values
167-
if null_kind == 3: # bit mask
168-
v = mbuf[i/8]
166+
if null_kind == ColumnNullType.USE_BITMASK:
167+
v = mbuf[i // 8]
169168
if null_value == 1:
170169
v = ~v
171170

172-
if v & (1<<(i%8)):
171+
if v & (1<<(i % 8)):
173172
str_list.append(np.nan)
174173
continue
175174

176-
elif null_kind == 4 and mbuf[i] == null_value: # byte mask
175+
elif null_kind == ColumnNullType.USE_BYTEMASK and mbuf[i] == null_value:
177176
str_list.append(np.nan)
178177
continue
179178

@@ -268,8 +267,7 @@ def __init__(self, column : pd.Series,
268267
Series/ndarray for now.
269268
"""
270269
if not isinstance(column, pd.Series):
271-
raise NotImplementedError("Columns of type {} not handled "
272-
"yet".format(type(column)))
270+
raise NotImplementedError(f"Columns of type {type(column)} not handled yet")
273271

274272
# Store the column as a private attribute
275273
self._col = column

0 commit comments

Comments
 (0)