|
| 1 | +"""An interface for extending pandas with custom arrays.""" |
| 2 | +import abc |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from pandas.compat import add_metaclass |
| 7 | + |
| 8 | + |
| 9 | +_not_implemented_message = "{} does not implement {}." |
| 10 | + |
| 11 | + |
| 12 | +@add_metaclass(abc.ABCMeta) |
| 13 | +class ExtensionArray(object): |
| 14 | + """Abstract base class for custom array types |
| 15 | +
|
| 16 | + pandas will recognize instances of this class as proper arrays |
| 17 | + with a custom type and will not attempt to coerce them to objects. |
| 18 | +
|
| 19 | + Subclasses are expected to implement the following methods. |
| 20 | + """ |
| 21 | + # ------------------------------------------------------------------------ |
| 22 | + # Must be a Sequence |
| 23 | + # ------------------------------------------------------------------------ |
| 24 | + @abc.abstractmethod |
| 25 | + def __getitem__(self, item): |
| 26 | + """Select a subset of self |
| 27 | +
|
| 28 | + Notes |
| 29 | + ----- |
| 30 | + As a sequence, __getitem__ should expect integer or slice ``key``. |
| 31 | +
|
| 32 | + For slice ``key``, you should return an instance of yourself, even |
| 33 | + if the slice is length 0 or 1. |
| 34 | +
|
| 35 | + For scalar ``key``, you may return a scalar suitable for your type. |
| 36 | + The scalar need not be an instance or subclass of your array type. |
| 37 | + """ |
| 38 | + # type (Any) -> Any |
| 39 | + |
| 40 | + def __setitem__(self, key, value): |
| 41 | + # type: (Any, Any) -> None |
| 42 | + raise NotImplementedError(_not_implemented_message.format( |
| 43 | + type(self), '__setitem__') |
| 44 | + ) |
| 45 | + |
| 46 | + @abc.abstractmethod |
| 47 | + def __iter__(self): |
| 48 | + # type: () -> Iterator |
| 49 | + pass |
| 50 | + |
| 51 | + @abc.abstractmethod |
| 52 | + def __len__(self): |
| 53 | + # type: () -> int |
| 54 | + pass |
| 55 | + |
| 56 | + # ------------------------------------------------------------------------ |
| 57 | + # Required attributes |
| 58 | + # ------------------------------------------------------------------------ |
| 59 | + @property |
| 60 | + def base(self): |
| 61 | + """The base array I am a view of. None by default.""" |
| 62 | + |
| 63 | + @property |
| 64 | + @abc.abstractmethod |
| 65 | + def dtype(self): |
| 66 | + """An instance of 'ExtensionDtype'.""" |
| 67 | + # type: () -> ExtensionDtype |
| 68 | + pass |
| 69 | + |
| 70 | + @property |
| 71 | + def shape(self): |
| 72 | + # type: () -> Tuple[int, ...] |
| 73 | + return (len(self),) |
| 74 | + |
| 75 | + @property |
| 76 | + def ndim(self): |
| 77 | + # type: () -> int |
| 78 | + """Extension Arrays are only allowed to be 1-dimensional.""" |
| 79 | + return 1 |
| 80 | + |
| 81 | + @property |
| 82 | + @abc.abstractmethod |
| 83 | + def nbytes(self): |
| 84 | + """The number of bytes needed to store this object in memory.""" |
| 85 | + # type: () -> int |
| 86 | + pass |
| 87 | + |
| 88 | + # ------------------------------------------------------------------------ |
| 89 | + # Additional Methods |
| 90 | + # ------------------------------------------------------------------------ |
| 91 | + @abc.abstractmethod |
| 92 | + def isna(self): |
| 93 | + """Boolean NumPy array indicating if each value is missing.""" |
| 94 | + # type: () -> np.ndarray |
| 95 | + pass |
| 96 | + |
| 97 | + # ------------------------------------------------------------------------ |
| 98 | + # Indexing methods |
| 99 | + # ------------------------------------------------------------------------ |
| 100 | + @abc.abstractmethod |
| 101 | + def take(self, indexer, allow_fill=True, fill_value=None): |
| 102 | + # type: (Sequence, bool, Optional[Any]) -> ExtensionArray |
| 103 | + """For slicing""" |
| 104 | + |
| 105 | + def take_nd(self, indexer, allow_fill=True, fill_value=None): |
| 106 | + """For slicing""" |
| 107 | + # TODO: this isn't really nescessary for 1-D |
| 108 | + return self.take(indexer, allow_fill=allow_fill, |
| 109 | + fill_value=fill_value) |
| 110 | + |
| 111 | + @abc.abstractmethod |
| 112 | + def copy(self, deep=False): |
| 113 | + # type: (bool) -> ExtensionArray |
| 114 | + """Return a copy of the array.""" |
| 115 | + |
| 116 | + # ------------------------------------------------------------------------ |
| 117 | + # Block-related methods |
| 118 | + # ------------------------------------------------------------------------ |
| 119 | + @property |
| 120 | + def _fill_value(self): |
| 121 | + """The missing value for this type, e.g. np.nan""" |
| 122 | + # type: () -> Any |
| 123 | + return None |
| 124 | + |
| 125 | + @abc.abstractmethod |
| 126 | + def _formatting_values(self): |
| 127 | + # type: () -> np.ndarray |
| 128 | + # At the moment, this has to be an array since we use result.dtype |
| 129 | + """An array of values to be printed in, e.g. the Series repr""" |
| 130 | + |
| 131 | + @classmethod |
| 132 | + @abc.abstractmethod |
| 133 | + def _concat_same_type(cls, to_concat): |
| 134 | + # type: (Sequence[ExtensionArray]) -> ExtensionArray |
| 135 | + """Concatenate multiple array |
| 136 | +
|
| 137 | + Parameters |
| 138 | + ---------- |
| 139 | + to_concat : sequence of this type |
| 140 | +
|
| 141 | + Returns |
| 142 | + ------- |
| 143 | + ExtensionArray |
| 144 | + """ |
| 145 | + |
| 146 | + @abc.abstractmethod |
| 147 | + def get_values(self): |
| 148 | + # type: () -> np.ndarray |
| 149 | + """Get the underlying values backing your data |
| 150 | + """ |
| 151 | + pass |
| 152 | + |
| 153 | + def _can_hold_na(self): |
| 154 | + """Whether your array can hold missing values. True by default. |
| 155 | +
|
| 156 | + Notes |
| 157 | + ----- |
| 158 | + Setting this to false will optimize some operations like fillna. |
| 159 | + """ |
| 160 | + # type: () -> bool |
| 161 | + return True |
| 162 | + |
| 163 | + @property |
| 164 | + def is_sparse(self): |
| 165 | + """Whether your array is sparse. True by default.""" |
| 166 | + # type: () -> bool |
| 167 | + return False |
| 168 | + |
| 169 | + def _slice(self, slicer): |
| 170 | + # type: (Union[tuple, Sequence, int]) -> 'ExtensionArray' |
| 171 | + """Return a new array sliced by `slicer`. |
| 172 | +
|
| 173 | + Parameters |
| 174 | + ---------- |
| 175 | + slicer : slice or np.ndarray |
| 176 | + If an array, it should just be a boolean mask |
| 177 | +
|
| 178 | + Returns |
| 179 | + ------- |
| 180 | + array : ExtensionArray |
| 181 | + Should return an ExtensionArray, even if ``self[slicer]`` |
| 182 | + would return a scalar. |
| 183 | + """ |
| 184 | + return type(self)(self[slicer]) |
| 185 | + |
| 186 | + def value_counts(self, dropna=True): |
| 187 | + """Optional method for computing the histogram of the counts. |
| 188 | +
|
| 189 | + Parameters |
| 190 | + ---------- |
| 191 | + dropna : bool, default True |
| 192 | + whether to exclude missing values from the computation |
| 193 | +
|
| 194 | + Returns |
| 195 | + ------- |
| 196 | + counts : Series |
| 197 | + """ |
| 198 | + from pandas.core.algorithms import value_counts |
| 199 | + mask = ~np.asarray(self.isna()) |
| 200 | + values = self[mask] # XXX: this imposes boolean indexing |
| 201 | + return value_counts(np.asarray(values), dropna=dropna) |
0 commit comments