|
| 1 | +""" |
| 2 | +Test extension array that has custom attribute information (not stored on the dtype). |
| 3 | +
|
| 4 | +""" |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import numbers |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from pandas._typing import type_t |
| 12 | + |
| 13 | +from pandas.core.dtypes.base import ExtensionDtype |
| 14 | + |
| 15 | +import pandas as pd |
| 16 | +from pandas.core.arrays import ExtensionArray |
| 17 | + |
| 18 | + |
| 19 | +class FloatAttrDtype(ExtensionDtype): |
| 20 | + type = float |
| 21 | + name = "float_attr" |
| 22 | + na_value = np.nan |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def construct_array_type(cls) -> type_t[FloatAttrArray]: |
| 26 | + """ |
| 27 | + Return the array type associated with this dtype. |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + type |
| 32 | + """ |
| 33 | + return FloatAttrArray |
| 34 | + |
| 35 | + |
| 36 | +class FloatAttrArray(ExtensionArray): |
| 37 | + dtype = FloatAttrDtype() |
| 38 | + __array_priority__ = 1000 |
| 39 | + |
| 40 | + def __init__(self, values, attr=None) -> None: |
| 41 | + if not isinstance(values, np.ndarray): |
| 42 | + raise TypeError("Need to pass a numpy array of float64 dtype as values") |
| 43 | + if not values.dtype == "float64": |
| 44 | + raise TypeError("Need to pass a numpy array of float64 dtype as values") |
| 45 | + self.data = values |
| 46 | + self.attr = attr |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def _from_sequence(cls, scalars, dtype=None, copy=False): |
| 50 | + data = np.array(scalars, dtype="float64", copy=copy) |
| 51 | + return cls(data) |
| 52 | + |
| 53 | + def __getitem__(self, item): |
| 54 | + if isinstance(item, numbers.Integral): |
| 55 | + return self.data[item] |
| 56 | + else: |
| 57 | + # slice, list-like, mask |
| 58 | + item = pd.api.indexers.check_array_indexer(self, item) |
| 59 | + return type(self)(self.data[item], self.attr) |
| 60 | + |
| 61 | + def __len__(self) -> int: |
| 62 | + return len(self.data) |
| 63 | + |
| 64 | + def isna(self): |
| 65 | + return np.isnan(self.data) |
| 66 | + |
| 67 | + def take(self, indexer, allow_fill=False, fill_value=None): |
| 68 | + from pandas.api.extensions import take |
| 69 | + |
| 70 | + data = self.data |
| 71 | + if allow_fill and fill_value is None: |
| 72 | + fill_value = self.dtype.na_value |
| 73 | + |
| 74 | + result = take(data, indexer, fill_value=fill_value, allow_fill=allow_fill) |
| 75 | + return type(self)(result, self.attr) |
| 76 | + |
| 77 | + def copy(self): |
| 78 | + return type(self)(self.data.copy(), self.attr) |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def _concat_same_type(cls, to_concat): |
| 82 | + data = np.concatenate([x.data for x in to_concat]) |
| 83 | + attr = to_concat[0].attr if len(to_concat) else None |
| 84 | + return cls(data, attr) |
0 commit comments