|
| 1 | +"""Rudimentary Apache Arrow-backed ExtensionArray. |
| 2 | +
|
| 3 | +At the moment, just a boolean array / type is implemented. |
| 4 | +Eventually, we'll want to parametrize the type and support |
| 5 | +multiple dtypes. Not all methods are implemented yet, and the |
| 6 | +current implementation is not efficient. |
| 7 | +""" |
| 8 | +import copy |
| 9 | +import itertools |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import pyarrow as pa |
| 13 | +import pandas as pd |
| 14 | +from pandas.api.extensions import ( |
| 15 | + ExtensionDtype, ExtensionArray, take, register_extension_dtype |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@register_extension_dtype |
| 20 | +class ArrowBoolDtype(ExtensionDtype): |
| 21 | + |
| 22 | + type = np.bool_ |
| 23 | + kind = 'b' |
| 24 | + name = 'arrow_bool' |
| 25 | + na_value = pa.NULL |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def construct_from_string(cls, string): |
| 29 | + if string == cls.name: |
| 30 | + return cls() |
| 31 | + else: |
| 32 | + raise TypeError("Cannot construct a '{}' from " |
| 33 | + "'{}'".format(cls, string)) |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def construct_array_type(cls): |
| 37 | + return ArrowBoolArray |
| 38 | + |
| 39 | + def _is_boolean(self): |
| 40 | + return True |
| 41 | + |
| 42 | + |
| 43 | +class ArrowBoolArray(ExtensionArray): |
| 44 | + def __init__(self, values): |
| 45 | + if not isinstance(values, pa.ChunkedArray): |
| 46 | + raise ValueError |
| 47 | + |
| 48 | + assert values.type == pa.bool_() |
| 49 | + self._data = values |
| 50 | + self._dtype = ArrowBoolDtype() |
| 51 | + |
| 52 | + def __repr__(self): |
| 53 | + return "ArrowBoolArray({})".format(repr(self._data)) |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def from_scalars(cls, values): |
| 57 | + arr = pa.chunked_array([pa.array(np.asarray(values))]) |
| 58 | + return cls(arr) |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def from_array(cls, arr): |
| 62 | + assert isinstance(arr, pa.Array) |
| 63 | + return cls(pa.chunked_array([arr])) |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def _from_sequence(cls, scalars, dtype=None, copy=False): |
| 67 | + return cls.from_scalars(scalars) |
| 68 | + |
| 69 | + def __getitem__(self, item): |
| 70 | + return self._data.to_pandas()[item] |
| 71 | + |
| 72 | + def __len__(self): |
| 73 | + return len(self._data) |
| 74 | + |
| 75 | + @property |
| 76 | + def dtype(self): |
| 77 | + return self._dtype |
| 78 | + |
| 79 | + @property |
| 80 | + def nbytes(self): |
| 81 | + return sum(x.size for chunk in self._data.chunks |
| 82 | + for x in chunk.buffers() |
| 83 | + if x is not None) |
| 84 | + |
| 85 | + def isna(self): |
| 86 | + return pd.isna(self._data.to_pandas()) |
| 87 | + |
| 88 | + def take(self, indices, allow_fill=False, fill_value=None): |
| 89 | + data = self._data.to_pandas() |
| 90 | + |
| 91 | + if allow_fill and fill_value is None: |
| 92 | + fill_value = self.dtype.na_value |
| 93 | + |
| 94 | + result = take(data, indices, fill_value=fill_value, |
| 95 | + allow_fill=allow_fill) |
| 96 | + return self._from_sequence(result, dtype=self.dtype) |
| 97 | + |
| 98 | + def copy(self, deep=False): |
| 99 | + if deep: |
| 100 | + return copy.deepcopy(self._data) |
| 101 | + else: |
| 102 | + return copy.copy(self._data) |
| 103 | + |
| 104 | + def _concat_same_type(cls, to_concat): |
| 105 | + chunks = list(itertools.chain.from_iterable(x._data.chunks |
| 106 | + for x in to_concat)) |
| 107 | + arr = pa.chunked_array(chunks) |
| 108 | + return cls(arr) |
0 commit comments