forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray.py
649 lines (551 loc) · 20.9 KB
/
array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
from __future__ import annotations
from typing import (
TYPE_CHECKING,
Any,
TypeVar,
)
import numpy as np
from pandas._typing import (
Dtype,
PositionalIndexer,
TakeIndexer,
npt,
)
from pandas.compat import (
pa_version_under1p01,
pa_version_under2p0,
pa_version_under5p0,
pa_version_under6p0,
)
from pandas.util._decorators import doc
from pandas.core.dtypes.common import (
is_array_like,
is_bool_dtype,
is_integer,
is_integer_dtype,
is_scalar,
)
from pandas.core.dtypes.missing import isna
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays.base import ExtensionArray
from pandas.core.indexers import (
check_array_indexer,
unpack_tuple_and_ellipses,
validate_indices,
)
if not pa_version_under1p01:
import pyarrow as pa
import pyarrow.compute as pc
from pandas.core.arrays.arrow._arrow_utils import fallback_performancewarning
from pandas.core.arrays.arrow.dtype import ArrowDtype
ARROW_CMP_FUNCS = {
"eq": pc.equal,
"ne": pc.not_equal,
"lt": pc.less,
"gt": pc.greater,
"le": pc.less_equal,
"ge": pc.greater_equal,
}
if TYPE_CHECKING:
from pandas import Series
ArrowExtensionArrayT = TypeVar("ArrowExtensionArrayT", bound="ArrowExtensionArray")
class ArrowExtensionArray(OpsMixin, ExtensionArray):
"""
Base class for ExtensionArray backed by Arrow ChunkedArray.
"""
_data: pa.ChunkedArray
def __init__(self, values: pa.Array | pa.ChunkedArray) -> None:
if pa_version_under1p01:
msg = "pyarrow>=1.0.0 is required for PyArrow backed ArrowExtensionArray."
raise ImportError(msg)
if isinstance(values, pa.Array):
self._data = pa.chunked_array([values])
elif isinstance(values, pa.ChunkedArray):
self._data = values
else:
raise ValueError(
f"Unsupported type '{type(values)}' for ArrowExtensionArray"
)
self._dtype = ArrowDtype(self._data.type)
@classmethod
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
"""
Construct a new ExtensionArray from a sequence of scalars.
"""
if isinstance(dtype, ArrowDtype):
pa_dtype = dtype.pyarrow_dtype
elif dtype:
pa_dtype = pa.from_numpy_dtype(dtype)
else:
pa_dtype = None
if isinstance(scalars, cls):
data = scalars._data
if pa_dtype:
data = data.cast(pa_dtype)
return cls(data)
else:
return cls(
pa.chunked_array(pa.array(scalars, type=pa_dtype, from_pandas=True))
)
@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Dtype | None = None, copy=False
):
"""
Construct a new ExtensionArray from a sequence of strings.
"""
return cls._from_sequence(strings, dtype=dtype, copy=copy)
def __getitem__(self, item: PositionalIndexer):
"""Select a subset of self.
Parameters
----------
item : int, slice, or ndarray
* int: The position in 'self' to get.
* slice: A slice object, where 'start', 'stop', and 'step' are
integers or None
* ndarray: A 1-d boolean NumPy ndarray the same length as 'self'
Returns
-------
item : scalar or ExtensionArray
Notes
-----
For scalar ``item``, return a scalar value suitable for the array's
type. This should be an instance of ``self.dtype.type``.
For slice ``key``, return an instance of ``ExtensionArray``, even
if the slice is length 0 or 1.
For a boolean mask, return an instance of ``ExtensionArray``, filtered
to the values where ``item`` is True.
"""
item = check_array_indexer(self, item)
if isinstance(item, np.ndarray):
if not len(item):
# Removable once we migrate StringDtype[pyarrow] to ArrowDtype[string]
if self._dtype.name == "string" and self._dtype.storage == "pyarrow":
pa_dtype = pa.string()
else:
pa_dtype = self._dtype.pyarrow_dtype
return type(self)(pa.chunked_array([], type=pa_dtype))
elif is_integer_dtype(item.dtype):
return self.take(item)
elif is_bool_dtype(item.dtype):
return type(self)(self._data.filter(item))
else:
raise IndexError(
"Only integers, slices and integer or "
"boolean arrays are valid indices."
)
elif isinstance(item, tuple):
item = unpack_tuple_and_ellipses(item)
# error: Non-overlapping identity check (left operand type:
# "Union[Union[int, integer[Any]], Union[slice, List[int],
# ndarray[Any, Any]]]", right operand type: "ellipsis")
if item is Ellipsis: # type: ignore[comparison-overlap]
# TODO: should be handled by pyarrow?
item = slice(None)
if is_scalar(item) and not is_integer(item):
# e.g. "foo" or 2.5
# exception message copied from numpy
raise IndexError(
r"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis "
r"(`None`) and integer or boolean arrays are valid indices"
)
# We are not an array indexer, so maybe e.g. a slice or integer
# indexer. We dispatch to pyarrow.
value = self._data[item]
if isinstance(value, pa.ChunkedArray):
return type(self)(value)
else:
scalar = value.as_py()
if scalar is None:
return self._dtype.na_value
else:
return scalar
def __arrow_array__(self, type=None):
"""Convert myself to a pyarrow ChunkedArray."""
return self._data
def _cmp_method(self, other, op):
from pandas.arrays import BooleanArray
pc_func = ARROW_CMP_FUNCS[op.__name__]
if isinstance(other, ArrowExtensionArray):
result = pc_func(self._data, other._data)
elif isinstance(other, (np.ndarray, list)):
result = pc_func(self._data, other)
elif is_scalar(other):
try:
result = pc_func(self._data, pa.scalar(other))
except (pa.lib.ArrowNotImplementedError, pa.lib.ArrowInvalid):
mask = isna(self) | isna(other)
valid = ~mask
result = np.zeros(len(self), dtype="bool")
result[valid] = op(np.array(self)[valid], other)
return BooleanArray(result, mask)
else:
return NotImplementedError(
f"{op.__name__} not implemented for {type(other)}"
)
if pa_version_under2p0:
result = result.to_pandas().values
else:
result = result.to_numpy()
return BooleanArray._from_sequence(result)
def equals(self, other) -> bool:
if not isinstance(other, ArrowExtensionArray):
return False
# I'm told that pyarrow makes __eq__ behave like pandas' equals;
# TODO: is this documented somewhere?
return self._data == other._data
@property
def dtype(self) -> ArrowDtype:
"""
An instance of 'ExtensionDtype'.
"""
return self._dtype
@property
def nbytes(self) -> int:
"""
The number of bytes needed to store this object in memory.
"""
return self._data.nbytes
def __len__(self) -> int:
"""
Length of this array.
Returns
-------
length : int
"""
return len(self._data)
def isna(self) -> npt.NDArray[np.bool_]:
"""
Boolean NumPy array indicating if each value is missing.
This should return a 1-D array the same length as 'self'.
"""
if pa_version_under2p0:
return self._data.is_null().to_pandas().values
else:
return self._data.is_null().to_numpy()
def copy(self: ArrowExtensionArrayT) -> ArrowExtensionArrayT:
"""
Return a shallow copy of the array.
Underlying ChunkedArray is immutable, so a deep copy is unnecessary.
Returns
-------
type(self)
"""
return type(self)(self._data)
def dropna(self: ArrowExtensionArrayT) -> ArrowExtensionArrayT:
"""
Return ArrowExtensionArray without NA values.
Returns
-------
ArrowExtensionArray
"""
if pa_version_under6p0:
fallback_performancewarning(version="6")
return super().dropna()
else:
return type(self)(pc.drop_null(self._data))
@doc(ExtensionArray.factorize)
def factorize(self, na_sentinel: int = -1) -> tuple[np.ndarray, ExtensionArray]:
encoded = self._data.dictionary_encode()
indices = pa.chunked_array(
[c.indices for c in encoded.chunks], type=encoded.type.index_type
).to_pandas()
if indices.dtype.kind == "f":
indices[np.isnan(indices)] = na_sentinel
indices = indices.astype(np.int64, copy=False)
if encoded.num_chunks:
uniques = type(self)(encoded.chunk(0).dictionary)
else:
uniques = type(self)(pa.array([], type=encoded.type.value_type))
return indices.values, uniques
def reshape(self, *args, **kwargs):
raise NotImplementedError(
f"{type(self)} does not support reshape "
f"as backed by a 1D pyarrow.ChunkedArray."
)
def take(
self,
indices: TakeIndexer,
allow_fill: bool = False,
fill_value: Any = None,
):
"""
Take elements from an array.
Parameters
----------
indices : sequence of int or one-dimensional np.ndarray of int
Indices to be taken.
allow_fill : bool, default False
How to handle negative values in `indices`.
* False: negative values in `indices` indicate positional indices
from the right (the default). This is similar to
:func:`numpy.take`.
* True: negative values in `indices` indicate
missing values. These values are set to `fill_value`. Any other
other negative values raise a ``ValueError``.
fill_value : any, optional
Fill value to use for NA-indices when `allow_fill` is True.
This may be ``None``, in which case the default NA value for
the type, ``self.dtype.na_value``, is used.
For many ExtensionArrays, there will be two representations of
`fill_value`: a user-facing "boxed" scalar, and a low-level
physical NA value. `fill_value` should be the user-facing version,
and the implementation should handle translating that to the
physical version for processing the take if necessary.
Returns
-------
ExtensionArray
Raises
------
IndexError
When the indices are out of bounds for the array.
ValueError
When `indices` contains negative values other than ``-1``
and `allow_fill` is True.
See Also
--------
numpy.take
api.extensions.take
Notes
-----
ExtensionArray.take is called by ``Series.__getitem__``, ``.loc``,
``iloc``, when `indices` is a sequence of values. Additionally,
it's called by :meth:`Series.reindex`, or any other method
that causes realignment, with a `fill_value`.
"""
# TODO: Remove once we got rid of the (indices < 0) check
if not is_array_like(indices):
indices_array = np.asanyarray(indices)
else:
# error: Incompatible types in assignment (expression has type
# "Sequence[int]", variable has type "ndarray")
indices_array = indices # type: ignore[assignment]
if len(self._data) == 0 and (indices_array >= 0).any():
raise IndexError("cannot do a non-empty take")
if indices_array.size > 0 and indices_array.max() >= len(self._data):
raise IndexError("out of bounds value in 'indices'.")
if allow_fill:
fill_mask = indices_array < 0
if fill_mask.any():
validate_indices(indices_array, len(self._data))
# TODO(ARROW-9433): Treat negative indices as NULL
indices_array = pa.array(indices_array, mask=fill_mask)
result = self._data.take(indices_array)
if isna(fill_value):
return type(self)(result)
# TODO: ArrowNotImplementedError: Function fill_null has no
# kernel matching input types (array[string], scalar[string])
result = type(self)(result)
result[fill_mask] = fill_value
return result
# return type(self)(pc.fill_null(result, pa.scalar(fill_value)))
else:
# Nothing to fill
return type(self)(self._data.take(indices))
else: # allow_fill=False
# TODO(ARROW-9432): Treat negative indices as indices from the right.
if (indices_array < 0).any():
# Don't modify in-place
indices_array = np.copy(indices_array)
indices_array[indices_array < 0] += len(self._data)
return type(self)(self._data.take(indices_array))
def unique(self: ArrowExtensionArrayT) -> ArrowExtensionArrayT:
"""
Compute the ArrowExtensionArray of unique values.
Returns
-------
ArrowExtensionArray
"""
if pa_version_under2p0:
fallback_performancewarning(version="2")
return super().unique()
else:
return type(self)(pc.unique(self._data))
def value_counts(self, dropna: bool = True) -> Series:
"""
Return a Series containing counts of each unique value.
Parameters
----------
dropna : bool, default True
Don't include counts of missing values.
Returns
-------
counts : Series
See Also
--------
Series.value_counts
"""
from pandas import (
Index,
Series,
)
vc = self._data.value_counts()
values = vc.field(0)
counts = vc.field(1)
if dropna and self._data.null_count > 0:
mask = values.is_valid()
values = values.filter(mask)
counts = counts.filter(mask)
# No missing values so we can adhere to the interface and return a numpy array.
counts = np.array(counts)
index = Index(type(self)(values))
return Series(counts, index=index).astype("Int64")
@classmethod
def _concat_same_type(
cls: type[ArrowExtensionArrayT], to_concat
) -> ArrowExtensionArrayT:
"""
Concatenate multiple ArrowExtensionArrays.
Parameters
----------
to_concat : sequence of ArrowExtensionArrays
Returns
-------
ArrowExtensionArray
"""
import pyarrow as pa
chunks = [array for ea in to_concat for array in ea._data.iterchunks()]
arr = pa.chunked_array(chunks)
return cls(arr)
def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None:
"""Set one or more values inplace.
Parameters
----------
key : int, ndarray, or slice
When called from, e.g. ``Series.__setitem__``, ``key`` will be
one of
* scalar int
* ndarray of integers.
* boolean ndarray
* slice object
value : ExtensionDtype.type, Sequence[ExtensionDtype.type], or object
value or values to be set of ``key``.
Returns
-------
None
"""
key = check_array_indexer(self, key)
indices = self._indexing_key_to_indices(key)
value = self._maybe_convert_setitem_value(value)
argsort = np.argsort(indices)
indices = indices[argsort]
if is_scalar(value):
value = np.broadcast_to(value, len(self))
elif len(indices) != len(value):
raise ValueError("Length of indexer and values mismatch")
else:
value = np.asarray(value)[argsort]
self._data = self._set_via_chunk_iteration(indices=indices, value=value)
def _indexing_key_to_indices(
self, key: int | slice | np.ndarray
) -> npt.NDArray[np.intp]:
"""
Convert indexing key for self into positional indices.
Parameters
----------
key : int | slice | np.ndarray
Returns
-------
npt.NDArray[np.intp]
"""
n = len(self)
if isinstance(key, slice):
indices = np.arange(n)[key]
elif is_integer(key):
# error: Invalid index type "List[Union[int, ndarray[Any, Any]]]"
# for "ndarray[Any, dtype[signedinteger[Any]]]"; expected type
# "Union[SupportsIndex, _SupportsArray[dtype[Union[bool_,
# integer[Any]]]], _NestedSequence[_SupportsArray[dtype[Union
# [bool_, integer[Any]]]]], _NestedSequence[Union[bool, int]]
# , Tuple[Union[SupportsIndex, _SupportsArray[dtype[Union[bool_
# , integer[Any]]]], _NestedSequence[_SupportsArray[dtype[Union
# [bool_, integer[Any]]]]], _NestedSequence[Union[bool, int]]], ...]]"
indices = np.arange(n)[[key]] # type: ignore[index]
elif is_bool_dtype(key):
key = np.asarray(key)
if len(key) != n:
raise ValueError("Length of indexer and values mismatch")
indices = key.nonzero()[0]
else:
key = np.asarray(key)
indices = np.arange(n)[key]
return indices
def _maybe_convert_setitem_value(self, value):
"""Maybe convert value to be pyarrow compatible."""
# TODO: Make more robust like ArrowStringArray._maybe_convert_setitem_value
return value
def _set_via_chunk_iteration(
self, indices: npt.NDArray[np.intp], value: npt.NDArray[Any]
) -> pa.ChunkedArray:
"""
Loop through the array chunks and set the new values while
leaving the chunking layout unchanged.
Parameters
----------
indices : npt.NDArray[np.intp]
Position indices for the underlying ChunkedArray.
value : ExtensionDtype.type, Sequence[ExtensionDtype.type], or object
value or values to be set of ``key``.
Notes
-----
Assumes that indices is sorted. Caller is responsible for sorting.
"""
new_data = []
stop = 0
for chunk in self._data.iterchunks():
start, stop = stop, stop + len(chunk)
if len(indices) == 0 or stop <= indices[0]:
new_data.append(chunk)
else:
n = int(np.searchsorted(indices, stop, side="left"))
c_ind = indices[:n] - start
indices = indices[n:]
n = len(c_ind)
c_value, value = value[:n], value[n:]
new_data.append(self._replace_with_indices(chunk, c_ind, c_value))
return pa.chunked_array(new_data)
@classmethod
def _replace_with_indices(
cls,
chunk: pa.Array,
indices: npt.NDArray[np.intp],
value: npt.NDArray[Any],
) -> pa.Array:
"""
Replace items selected with a set of positional indices.
Analogous to pyarrow.compute.replace_with_mask, except that replacement
positions are identified via indices rather than a mask.
Parameters
----------
chunk : pa.Array
indices : npt.NDArray[np.intp]
value : npt.NDArray[Any]
Replacement value(s).
Returns
-------
pa.Array
"""
n = len(indices)
if n == 0:
return chunk
start, stop = indices[[0, -1]]
if (stop - start) == (n - 1):
# fast path for a contiguous set of indices
arrays = [
chunk[:start],
pa.array(value, type=chunk.type, from_pandas=True),
chunk[stop + 1 :],
]
arrays = [arr for arr in arrays if len(arr)]
if len(arrays) == 1:
return arrays[0]
return pa.concat_arrays(arrays)
mask = np.zeros(len(chunk), dtype=np.bool_)
mask[indices] = True
if pa_version_under5p0:
arr = chunk.to_numpy(zero_copy_only=False)
arr[mask] = value
return pa.array(arr, type=chunk.type)
if isna(value).all():
return pc.if_else(mask, None, chunk)
return pc.replace_with_mask(chunk, mask, value)