|
| 1 | +""" |
| 2 | +Test extension array for storing nested data in a pandas container. |
| 3 | +
|
| 4 | +The ListArray stores an ndarray of lists. |
| 5 | +""" |
| 6 | +import numbers |
| 7 | +import random |
| 8 | +import string |
| 9 | + |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +from pandas.core.dtypes.base import ExtensionDtype |
| 13 | + |
| 14 | +import pandas as pd |
| 15 | +from pandas.core.arrays import ExtensionArray |
| 16 | + |
| 17 | + |
| 18 | +class ListDtype(ExtensionDtype): |
| 19 | + type = list |
| 20 | + name = "list" |
| 21 | + na_value = np.nan |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def construct_array_type(cls): |
| 25 | + """ |
| 26 | + Return the array type associated with this dtype. |
| 27 | +
|
| 28 | + Returns |
| 29 | + ------- |
| 30 | + type |
| 31 | + """ |
| 32 | + return ListArray |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def construct_from_string(cls, string): |
| 36 | + if string == cls.name: |
| 37 | + return cls() |
| 38 | + else: |
| 39 | + raise TypeError("Cannot construct a '{}' from '{}'".format(cls, string)) |
| 40 | + |
| 41 | + |
| 42 | +class ListArray(ExtensionArray): |
| 43 | + dtype = ListDtype() |
| 44 | + __array_priority__ = 1000 |
| 45 | + |
| 46 | + def __init__(self, values, dtype=None, copy=False): |
| 47 | + if not isinstance(values, np.ndarray): |
| 48 | + raise TypeError("Need to pass a numpy array as values") |
| 49 | + for val in values: |
| 50 | + if not isinstance(val, self.dtype.type) and not pd.isna(val): |
| 51 | + raise TypeError("All values must be of type " + str(self.dtype.type)) |
| 52 | + self.data = values |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def _from_sequence(cls, scalars, dtype=None, copy=False): |
| 56 | + data = np.empty(len(scalars), dtype=object) |
| 57 | + data[:] = scalars |
| 58 | + return cls(data) |
| 59 | + |
| 60 | + def __getitem__(self, item): |
| 61 | + if isinstance(item, numbers.Integral): |
| 62 | + return self.data[item] |
| 63 | + else: |
| 64 | + # slice, list-like, mask |
| 65 | + return type(self)(self.data[item]) |
| 66 | + |
| 67 | + def __len__(self) -> int: |
| 68 | + return len(self.data) |
| 69 | + |
| 70 | + def isna(self): |
| 71 | + return np.array( |
| 72 | + [not isinstance(x, list) and np.isnan(x) for x in self.data], dtype=bool |
| 73 | + ) |
| 74 | + |
| 75 | + def take(self, indexer, allow_fill=False, fill_value=None): |
| 76 | + # re-implement here, since NumPy has trouble setting |
| 77 | + # sized objects like UserDicts into scalar slots of |
| 78 | + # an ndarary. |
| 79 | + indexer = np.asarray(indexer) |
| 80 | + msg = ( |
| 81 | + "Index is out of bounds or cannot do a " |
| 82 | + "non-empty take from an empty array." |
| 83 | + ) |
| 84 | + |
| 85 | + if allow_fill: |
| 86 | + if fill_value is None: |
| 87 | + fill_value = self.dtype.na_value |
| 88 | + # bounds check |
| 89 | + if (indexer < -1).any(): |
| 90 | + raise ValueError |
| 91 | + try: |
| 92 | + output = [ |
| 93 | + self.data[loc] if loc != -1 else fill_value for loc in indexer |
| 94 | + ] |
| 95 | + except IndexError: |
| 96 | + raise IndexError(msg) |
| 97 | + else: |
| 98 | + try: |
| 99 | + output = [self.data[loc] for loc in indexer] |
| 100 | + except IndexError: |
| 101 | + raise IndexError(msg) |
| 102 | + |
| 103 | + return self._from_sequence(output) |
| 104 | + |
| 105 | + def copy(self): |
| 106 | + return type(self)(self.data[:]) |
| 107 | + |
| 108 | + def astype(self, dtype, copy=True): |
| 109 | + if isinstance(dtype, type(self.dtype)) and dtype == self.dtype: |
| 110 | + if copy: |
| 111 | + return self.copy() |
| 112 | + return self |
| 113 | + elif pd.api.types.is_string_dtype(dtype) and not pd.api.types.is_object_dtype( |
| 114 | + dtype |
| 115 | + ): |
| 116 | + # numpy has problems with astype(str) for nested elements |
| 117 | + return np.array([str(x) for x in self.data], dtype=dtype) |
| 118 | + return np.array(self.data, dtype=dtype, copy=copy) |
| 119 | + |
| 120 | + @classmethod |
| 121 | + def _concat_same_type(cls, to_concat): |
| 122 | + data = np.concatenate([x.data for x in to_concat]) |
| 123 | + return cls(data) |
| 124 | + |
| 125 | + |
| 126 | +def make_data(): |
| 127 | + # TODO: Use a regular dict. See _NDFrameIndexer._setitem_with_indexer |
| 128 | + data = np.empty(100, dtype=object) |
| 129 | + data[:] = [ |
| 130 | + [random.choice(string.ascii_letters) for _ in range(random.randint(0, 10))] |
| 131 | + for _ in range(100) |
| 132 | + ] |
| 133 | + return data |
0 commit comments