Wrapper around array-like objects to create a new indexable object where values, when accessed, are automatically stacked along the last dimension. >>> indexer = indexing.BasicIndexer((slice(None),)) >>> np.array(StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[indexer]) arr
| 252 | |
| 253 | |
| 254 | class StackedBytesArray(indexing.ExplicitlyIndexedNDArrayMixin): |
| 255 | """Wrapper around array-like objects to create a new indexable object where |
| 256 | values, when accessed, are automatically stacked along the last dimension. |
| 257 | |
| 258 | >>> indexer = indexing.BasicIndexer((slice(None),)) |
| 259 | >>> np.array(StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[indexer]) |
| 260 | array(b'abc', dtype='|S3') |
| 261 | """ |
| 262 | |
| 263 | def __init__(self, array): |
| 264 | """ |
| 265 | Parameters |
| 266 | ---------- |
| 267 | array : array-like |
| 268 | Original array of values to wrap. |
| 269 | """ |
| 270 | if array.dtype != "S1": |
| 271 | raise ValueError( |
| 272 | "can only use StackedBytesArray if argument has dtype='S1'" |
| 273 | ) |
| 274 | self.array = indexing.as_indexable(array) |
| 275 | |
| 276 | @property |
| 277 | def dtype(self): |
| 278 | return np.dtype("S" + str(self.array.shape[-1])) |
| 279 | |
| 280 | @property |
| 281 | def shape(self) -> tuple[int, ...]: |
| 282 | return self.array.shape[:-1] |
| 283 | |
| 284 | def __repr__(self): |
| 285 | return f"{type(self).__name__}({self.array!r})" |
| 286 | |
| 287 | def _vindex_get(self, key): |
| 288 | return type(self)(self.array.vindex[key]) |
| 289 | |
| 290 | def _oindex_get(self, key): |
| 291 | return type(self)(self.array.oindex[key]) |
| 292 | |
| 293 | def __getitem__(self, key): |
| 294 | # require slicing the last dimension completely |
| 295 | key = type(key)(indexing.expanded_indexer(key.tuple, self.array.ndim)) |
| 296 | if key.tuple[-1] != slice(None): |
| 297 | raise IndexError("too many indices") |
| 298 | return type(self)(self.array[key]) |
| 299 | |
| 300 | def get_duck_array(self): |
| 301 | return _numpy_char_to_bytes(self.array.get_duck_array()) |
no outgoing calls
no test coverage detected
searching dependent graphs…