-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
/
Copy pathbase.py
2839 lines (2402 loc) · 92.7 KB
/
base.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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
An interface for extending pandas with custom arrays.
.. warning::
This is an experimental API and subject to breaking changes
without warning.
"""
from __future__ import annotations
import operator
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Literal,
cast,
overload,
)
import warnings
import numpy as np
from pandas._libs import (
algos as libalgos,
lib,
)
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import (
Appender,
Substitution,
cache_readonly,
)
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import (
validate_bool_kwarg,
validate_insert_loc,
)
from pandas.core.dtypes.cast import maybe_cast_pointwise_result
from pandas.core.dtypes.common import (
is_list_like,
is_scalar,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCIndex,
ABCSeries,
)
from pandas.core.dtypes.missing import isna
from pandas.core import (
arraylike,
missing,
roperator,
)
from pandas.core.algorithms import (
duplicated,
factorize_array,
isin,
map_array,
mode,
rank,
unique,
)
from pandas.core.array_algos.quantile import quantile_with_mask
from pandas.core.missing import _fill_limit_area_1d
from pandas.core.sorting import (
nargminmax,
nargsort,
)
if TYPE_CHECKING:
from collections.abc import (
Callable,
Iterator,
Sequence,
)
from pandas._libs.missing import NAType
from pandas._typing import (
ArrayLike,
AstypeArg,
AxisInt,
Dtype,
DtypeObj,
FillnaOptions,
InterpolateOptions,
NumpySorter,
NumpyValueArrayLike,
PositionalIndexer,
ScalarIndexer,
Self,
SequenceIndexer,
Shape,
SortKind,
TakeIndexer,
npt,
)
from pandas import Index
_extension_array_shared_docs: dict[str, str] = {}
class ExtensionArray:
"""
Abstract base class for custom 1-D array types.
pandas will recognize instances of this class as proper arrays
with a custom type and will not attempt to coerce them to objects. They
may be stored directly inside a :class:`DataFrame` or :class:`Series`.
Attributes
----------
dtype
nbytes
ndim
shape
Methods
-------
argsort
astype
copy
dropna
duplicated
factorize
fillna
equals
insert
interpolate
isin
isna
ravel
repeat
searchsorted
shift
take
tolist
unique
view
_accumulate
_concat_same_type
_explode
_formatter
_from_factorized
_from_sequence
_from_sequence_of_strings
_hash_pandas_object
_pad_or_backfill
_reduce
_values_for_argsort
_values_for_factorize
See Also
--------
api.extensions.ExtensionDtype : A custom data type, to be paired with an
ExtensionArray.
api.extensions.ExtensionArray.dtype : An instance of ExtensionDtype.
Notes
-----
The interface includes the following abstract methods that must be
implemented by subclasses:
* _from_sequence
* _from_factorized
* __getitem__
* __len__
* __eq__
* dtype
* nbytes
* isna
* take
* copy
* _concat_same_type
* interpolate
A default repr displaying the type, (truncated) data, length,
and dtype is provided. It can be customized or replaced by
by overriding:
* __repr__ : A default repr for the ExtensionArray.
* _formatter : Print scalars inside a Series or DataFrame.
Some methods require casting the ExtensionArray to an ndarray of Python
objects with ``self.astype(object)``, which may be expensive. When
performance is a concern, we highly recommend overriding the following
methods:
* fillna
* _pad_or_backfill
* dropna
* unique
* factorize / _values_for_factorize
* argsort, argmax, argmin / _values_for_argsort
* searchsorted
* map
The remaining methods implemented on this class should be performant,
as they only compose abstract methods. Still, a more efficient
implementation may be available, and these methods can be overridden.
One can implement methods to handle array accumulations or reductions.
* _accumulate
* _reduce
One can implement methods to handle parsing from strings that will be used
in methods such as ``pandas.io.parsers.read_csv``.
* _from_sequence_of_strings
This class does not inherit from 'abc.ABCMeta' for performance reasons.
Methods and properties required by the interface raise
``pandas.errors.AbstractMethodError`` and no ``register`` method is
provided for registering virtual subclasses.
ExtensionArrays are limited to 1 dimension.
They may be backed by none, one, or many NumPy arrays. For example,
``pandas.Categorical`` is an extension array backed by two arrays,
one for codes and one for categories. An array of IPv6 address may
be backed by a NumPy structured array with two fields, one for the
lower 64 bits and one for the upper 64 bits. Or they may be backed
by some other storage type, like Python lists. Pandas makes no
assumptions on how the data are stored, just that it can be converted
to a NumPy array.
The ExtensionArray interface does not impose any rules on how this data
is stored. However, currently, the backing data cannot be stored in
attributes called ``.values`` or ``._values`` to ensure full compatibility
with pandas internals. But other names as ``.data``, ``._data``,
``._items``, ... can be freely used.
If implementing NumPy's ``__array_ufunc__`` interface, pandas expects
that
1. You defer by returning ``NotImplemented`` when any Series are present
in `inputs`. Pandas will extract the arrays and call the ufunc again.
2. You define a ``_HANDLED_TYPES`` tuple as an attribute on the class.
Pandas inspect this to determine whether the ufunc is valid for the
types present.
See :ref:`extending.extension.ufunc` for more.
By default, ExtensionArrays are not hashable. Immutable subclasses may
override this behavior.
Examples
--------
Please see the following:
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/list/array.py
"""
# '_typ' is for pandas.core.dtypes.generic.ABCExtensionArray.
# Don't override this.
_typ = "extension"
# similar to __array_priority__, positions ExtensionArray after Index,
# Series, and DataFrame. EA subclasses may override to choose which EA
# subclass takes priority. If overriding, the value should always be
# strictly less than 2000 to be below Index.__pandas_priority__.
__pandas_priority__ = 1000
# ------------------------------------------------------------------------
# Constructors
# ------------------------------------------------------------------------
@classmethod
def _from_sequence(
cls, scalars, *, dtype: Dtype | None = None, copy: bool = False
) -> Self:
"""
Construct a new ExtensionArray from a sequence of scalars.
Parameters
----------
scalars : Sequence
Each element will be an instance of the scalar type for this
array, ``cls.dtype.type`` or be converted into this type in this method.
dtype : dtype, optional
Construct for this particular dtype. This should be a Dtype
compatible with the ExtensionArray.
copy : bool, default False
If True, copy the underlying data.
Returns
-------
ExtensionArray
See Also
--------
api.extensions.ExtensionArray._from_sequence_of_strings : Construct a new
ExtensionArray from a sequence of strings.
api.extensions.ExtensionArray._hash_pandas_object : Hook for
hash_pandas_object.
Examples
--------
>>> pd.arrays.IntegerArray._from_sequence([4, 5])
<IntegerArray>
[4, 5]
Length: 2, dtype: Int64
"""
raise AbstractMethodError(cls)
@classmethod
def _from_scalars(cls, scalars, *, dtype: DtypeObj) -> Self:
"""
Strict analogue to _from_sequence, allowing only sequences of scalars
that should be specifically inferred to the given dtype.
Parameters
----------
scalars : sequence
dtype : ExtensionDtype
Raises
------
TypeError or ValueError
Notes
-----
This is called in a try/except block when casting the result of a
pointwise operation.
"""
try:
return cls._from_sequence(scalars, dtype=dtype, copy=False)
except (ValueError, TypeError):
raise
except Exception:
warnings.warn(
"_from_scalars should only raise ValueError or TypeError. "
"Consider overriding _from_scalars where appropriate.",
stacklevel=find_stack_level(),
)
raise
@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: ExtensionDtype, copy: bool = False
) -> Self:
"""
Construct a new ExtensionArray from a sequence of strings.
Parameters
----------
strings : Sequence
Each element will be an instance of the scalar type for this
array, ``cls.dtype.type``.
dtype : ExtensionDtype
Construct for this particular dtype. This should be a Dtype
compatible with the ExtensionArray.
copy : bool, default False
If True, copy the underlying data.
Returns
-------
ExtensionArray
See Also
--------
api.extensions.ExtensionArray._from_sequence : Construct a new ExtensionArray
from a sequence of scalars.
api.extensions.ExtensionArray._from_factorized : Reconstruct an ExtensionArray
after factorization.
api.extensions.ExtensionArray._from_scalars : Strict analogue to _from_sequence,
allowing only sequences of scalars that should be specifically inferred to
the given dtype.
Examples
--------
>>> pd.arrays.IntegerArray._from_sequence_of_strings(
... ["1", "2", "3"], dtype=pd.Int64Dtype()
... )
<IntegerArray>
[1, 2, 3]
Length: 3, dtype: Int64
"""
raise AbstractMethodError(cls)
@classmethod
def _from_factorized(cls, values, original):
"""
Reconstruct an ExtensionArray after factorization.
Parameters
----------
values : ndarray
An integer ndarray with the factorized values.
original : ExtensionArray
The original ExtensionArray that factorize was called on.
See Also
--------
factorize : Top-level factorize method that dispatches here.
ExtensionArray.factorize : Encode the extension array as an enumerated type.
Examples
--------
>>> interv_arr = pd.arrays.IntervalArray(
... [pd.Interval(0, 1), pd.Interval(1, 5), pd.Interval(1, 5)]
... )
>>> codes, uniques = pd.factorize(interv_arr)
>>> pd.arrays.IntervalArray._from_factorized(uniques, interv_arr)
<IntervalArray>
[(0, 1], (1, 5]]
Length: 2, dtype: interval[int64, right]
"""
raise AbstractMethodError(cls)
# ------------------------------------------------------------------------
# Must be a Sequence
# ------------------------------------------------------------------------
@overload
def __getitem__(self, item: ScalarIndexer) -> Any: ...
@overload
def __getitem__(self, item: SequenceIndexer) -> Self: ...
def __getitem__(self, item: PositionalIndexer) -> Self | Any:
"""
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'
* list[int]: A list of int
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.
"""
raise AbstractMethodError(self)
def __setitem__(self, key, value) -> None:
"""
Set one or more values inplace.
This method is not required to satisfy the pandas extension array
interface.
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
"""
# Some notes to the ExtensionArray implementer who may have ended up
# here. While this method is not required for the interface, if you
# *do* choose to implement __setitem__, then some semantics should be
# observed:
#
# * Setting multiple values : ExtensionArrays should support setting
# multiple values at once, 'key' will be a sequence of integers and
# 'value' will be a same-length sequence.
#
# * Broadcasting : For a sequence 'key' and a scalar 'value',
# each position in 'key' should be set to 'value'.
#
# * Coercion : Most users will expect basic coercion to work. For
# example, a string like '2018-01-01' is coerced to a datetime
# when setting on a datetime64ns array. In general, if the
# __init__ method coerces that value, then so should __setitem__
# Note, also, that Series/DataFrame.where internally use __setitem__
# on a copy of the data.
raise NotImplementedError(f"{type(self)} does not implement __setitem__.")
def __len__(self) -> int:
"""
Length of this array
Returns
-------
length : int
"""
raise AbstractMethodError(self)
def __iter__(self) -> Iterator[Any]:
"""
Iterate over elements of the array.
"""
# This needs to be implemented so that pandas recognizes extension
# arrays as list-like. The default implementation makes successive
# calls to ``__getitem__``, which may be slower than necessary.
for i in range(len(self)):
yield self[i]
def __contains__(self, item: object) -> bool | np.bool_:
"""
Return for `item in self`.
"""
# GH37867
# comparisons of any item to pd.NA always return pd.NA, so e.g. "a" in [pd.NA]
# would raise a TypeError. The implementation below works around that.
if is_scalar(item) and isna(item):
if not self._can_hold_na:
return False
elif item is self.dtype.na_value or isinstance(item, self.dtype.type):
return self._hasna
else:
return False
else:
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
# attribute "any"
return (item == self).any() # type: ignore[union-attr]
# error: Signature of "__eq__" incompatible with supertype "object"
def __eq__(self, other: object) -> ArrayLike: # type: ignore[override]
"""
Return for `self == other` (element-wise equality).
"""
# Implementer note: this should return a boolean numpy ndarray or
# a boolean ExtensionArray.
# When `other` is one of Series, Index, or DataFrame, this method should
# return NotImplemented (to ensure that those objects are responsible for
# first unpacking the arrays, and then dispatch the operation to the
# underlying arrays)
raise AbstractMethodError(self)
# error: Signature of "__ne__" incompatible with supertype "object"
def __ne__(self, other: object) -> ArrayLike: # type: ignore[override]
"""
Return for `self != other` (element-wise in-equality).
"""
# error: Unsupported operand type for ~ ("ExtensionArray")
return ~(self == other) # type: ignore[operator]
def to_numpy(
self,
dtype: npt.DTypeLike | None = None,
copy: bool = False,
na_value: object = lib.no_default,
) -> np.ndarray:
"""
Convert to a NumPy ndarray.
This is similar to :meth:`numpy.asarray`, but may provide additional control
over how the conversion is done.
Parameters
----------
dtype : str or numpy.dtype, optional
The dtype to pass to :meth:`numpy.asarray`.
copy : bool, default False
Whether to ensure that the returned value is a not a view on
another array. Note that ``copy=False`` does not *ensure* that
``to_numpy()`` is no-copy. Rather, ``copy=True`` ensure that
a copy is made, even if not strictly necessary.
na_value : Any, optional
The value to use for missing values. The default value depends
on `dtype` and the type of the array.
Returns
-------
numpy.ndarray
"""
result = np.asarray(self, dtype=dtype)
if copy or na_value is not lib.no_default:
result = result.copy()
if na_value is not lib.no_default:
result[self.isna()] = na_value
return result
# ------------------------------------------------------------------------
# Required attributes
# ------------------------------------------------------------------------
@property
def dtype(self) -> ExtensionDtype:
"""
An instance of ExtensionDtype.
See Also
--------
api.extensions.ExtensionDtype : Base class for extension dtypes.
api.extensions.ExtensionArray : Base class for extension array types.
api.extensions.ExtensionArray.dtype : The dtype of an ExtensionArray.
Series.dtype : The dtype of a Series.
DataFrame.dtype : The dtype of a DataFrame.
Examples
--------
>>> pd.array([1, 2, 3]).dtype
Int64Dtype()
"""
raise AbstractMethodError(self)
@property
def shape(self) -> Shape:
"""
Return a tuple of the array dimensions.
See Also
--------
numpy.ndarray.shape : Similar attribute which returns the shape of an array.
DataFrame.shape : Return a tuple representing the dimensionality of the
DataFrame.
Series.shape : Return a tuple representing the dimensionality of the Series.
Examples
--------
>>> arr = pd.array([1, 2, 3])
>>> arr.shape
(3,)
"""
return (len(self),)
@property
def size(self) -> int:
"""
The number of elements in the array.
"""
# error: Incompatible return value type (got "signedinteger[_64Bit]",
# expected "int") [return-value]
return np.prod(self.shape) # type: ignore[return-value]
@property
def ndim(self) -> int:
"""
Extension Arrays are only allowed to be 1-dimensional.
See Also
--------
ExtensionArray.shape: Return a tuple of the array dimensions.
ExtensionArray.size: The number of elements in the array.
Examples
--------
>>> arr = pd.array([1, 2, 3])
>>> arr.ndim
1
"""
return 1
@property
def nbytes(self) -> int:
"""
The number of bytes needed to store this object in memory.
See Also
--------
ExtensionArray.shape: Return a tuple of the array dimensions.
ExtensionArray.size: The number of elements in the array.
Examples
--------
>>> pd.array([1, 2, 3]).nbytes
27
"""
# If this is expensive to compute, return an approximate lower bound
# on the number of bytes needed.
raise AbstractMethodError(self)
# ------------------------------------------------------------------------
# Additional Methods
# ------------------------------------------------------------------------
@overload
def astype(self, dtype: npt.DTypeLike, copy: bool = ...) -> np.ndarray: ...
@overload
def astype(self, dtype: ExtensionDtype, copy: bool = ...) -> ExtensionArray: ...
@overload
def astype(self, dtype: AstypeArg, copy: bool = ...) -> ArrayLike: ...
def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
"""
Cast to a NumPy array or ExtensionArray with 'dtype'.
Parameters
----------
dtype : str or dtype
Typecode or data-type to which the array is cast.
copy : bool, default True
Whether to copy the data, even if not necessary. If False,
a copy is made only if the old dtype does not match the
new dtype.
Returns
-------
np.ndarray or pandas.api.extensions.ExtensionArray
An ``ExtensionArray`` if ``dtype`` is ``ExtensionDtype``,
otherwise a Numpy ndarray with ``dtype`` for its dtype.
See Also
--------
Series.astype : Cast a Series to a different dtype.
DataFrame.astype : Cast a DataFrame to a different dtype.
api.extensions.ExtensionArray : Base class for ExtensionArray objects.
core.arrays.DatetimeArray._from_sequence : Create a DatetimeArray from a
sequence.
core.arrays.TimedeltaArray._from_sequence : Create a TimedeltaArray from
a sequence.
Examples
--------
>>> arr = pd.array([1, 2, 3])
>>> arr
<IntegerArray>
[1, 2, 3]
Length: 3, dtype: Int64
Casting to another ``ExtensionDtype`` returns an ``ExtensionArray``:
>>> arr1 = arr.astype("Float64")
>>> arr1
<FloatingArray>
[1.0, 2.0, 3.0]
Length: 3, dtype: Float64
>>> arr1.dtype
Float64Dtype()
Otherwise, we will get a Numpy ndarray:
>>> arr2 = arr.astype("float64")
>>> arr2
array([1., 2., 3.])
>>> arr2.dtype
dtype('float64')
"""
dtype = pandas_dtype(dtype)
if dtype == self.dtype:
if not copy:
return self
else:
return self.copy()
if isinstance(dtype, ExtensionDtype):
cls = dtype.construct_array_type()
return cls._from_sequence(self, dtype=dtype, copy=copy)
elif lib.is_np_dtype(dtype, "M"):
from pandas.core.arrays import DatetimeArray
return DatetimeArray._from_sequence(self, dtype=dtype, copy=copy)
elif lib.is_np_dtype(dtype, "m"):
from pandas.core.arrays import TimedeltaArray
return TimedeltaArray._from_sequence(self, dtype=dtype, copy=copy)
if not copy:
return np.asarray(self, dtype=dtype)
else:
return np.array(self, dtype=dtype, copy=copy)
def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
"""
A 1-D array indicating if each value is missing.
Returns
-------
numpy.ndarray or pandas.api.extensions.ExtensionArray
In most cases, this should return a NumPy ndarray. For
exceptional cases like ``SparseArray``, where returning
an ndarray would be expensive, an ExtensionArray may be
returned.
See Also
--------
ExtensionArray.dropna: Return ExtensionArray without NA values.
ExtensionArray.fillna: Fill NA/NaN values using the specified method.
Notes
-----
If returning an ExtensionArray, then
* ``na_values._is_boolean`` should be True
* ``na_values`` should implement :func:`ExtensionArray._reduce`
* ``na_values`` should implement :func:`ExtensionArray._accumulate`
* ``na_values.any`` and ``na_values.all`` should be implemented
Examples
--------
>>> arr = pd.array([1, 2, np.nan, np.nan])
>>> arr.isna()
array([False, False, True, True])
"""
raise AbstractMethodError(self)
@property
def _hasna(self) -> bool:
# GH#22680
"""
Equivalent to `self.isna().any()`.
Some ExtensionArray subclasses may be able to optimize this check.
"""
return bool(self.isna().any())
def _values_for_argsort(self) -> np.ndarray:
"""
Return values for sorting.
Returns
-------
ndarray
The transformed values should maintain the ordering between values
within the array.
See Also
--------
ExtensionArray.argsort : Return the indices that would sort this array.
Notes
-----
The caller is responsible for *not* modifying these values in-place, so
it is safe for implementers to give views on ``self``.
Functions that use this (e.g. ``ExtensionArray.argsort``) should ignore
entries with missing values in the original array (according to
``self.isna()``). This means that the corresponding entries in the returned
array don't need to be modified to sort correctly.
Examples
--------
In most cases, this is the underlying Numpy array of the ``ExtensionArray``:
>>> arr = pd.array([1, 2, 3])
>>> arr._values_for_argsort()
array([1, 2, 3])
"""
# Note: this is used in `ExtensionArray.argsort/argmin/argmax`.
return np.array(self)
def argsort(
self,
*,
ascending: bool = True,
kind: SortKind = "quicksort",
na_position: str = "last",
**kwargs,
) -> np.ndarray:
"""
Return the indices that would sort this array.
Parameters
----------
ascending : bool, default True
Whether the indices should result in an ascending
or descending sort.
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
Sorting algorithm.
na_position : {'first', 'last'}, default 'last'
If ``'first'``, put ``NaN`` values at the beginning.
If ``'last'``, put ``NaN`` values at the end.
**kwargs
Passed through to :func:`numpy.argsort`.
Returns
-------
np.ndarray[np.intp]
Array of indices that sort ``self``. If NaN values are contained,
NaN values are placed at the end.
See Also
--------
numpy.argsort : Sorting implementation used internally.
Examples
--------
>>> arr = pd.array([3, 1, 2, 5, 4])
>>> arr.argsort()
array([1, 2, 0, 4, 3])
"""
# Implementer note: You have two places to override the behavior of
# argsort.
# 1. _values_for_argsort : construct the values passed to np.argsort
# 2. argsort : total control over sorting. In case of overriding this,
# it is recommended to also override argmax/argmin
ascending = nv.validate_argsort_with_ascending(ascending, (), kwargs)
values = self._values_for_argsort()
return nargsort(
values,
kind=kind,
ascending=ascending,
na_position=na_position,
mask=np.asarray(self.isna()),
)
def argmin(self, skipna: bool = True) -> int:
"""
Return the index of minimum value.
In case of multiple occurrences of the minimum value, the index
corresponding to the first occurrence is returned.
Parameters
----------
skipna : bool, default True
Returns
-------
int
See Also
--------
ExtensionArray.argmax : Return the index of the maximum value.
Examples
--------
>>> arr = pd.array([3, 1, 2, 5, 4])
>>> arr.argmin()
1
"""
# Implementer note: You have two places to override the behavior of
# argmin.
# 1. _values_for_argsort : construct the values used in nargminmax
# 2. argmin itself : total control over sorting.
validate_bool_kwarg(skipna, "skipna")
if not skipna and self._hasna:
raise ValueError("Encountered an NA value with skipna=False")
return nargminmax(self, "argmin")
def argmax(self, skipna: bool = True) -> int:
"""
Return the index of maximum value.
In case of multiple occurrences of the maximum value, the index
corresponding to the first occurrence is returned.
Parameters
----------
skipna : bool, default True
Returns
-------
int
See Also
--------
ExtensionArray.argmin : Return the index of the minimum value.
Examples
--------
>>> arr = pd.array([3, 1, 2, 5, 4])
>>> arr.argmax()
3
"""
# Implementer note: You have two places to override the behavior of
# argmax.
# 1. _values_for_argsort : construct the values used in nargminmax
# 2. argmax itself : total control over sorting.
validate_bool_kwarg(skipna, "skipna")
if not skipna and self._hasna:
raise ValueError("Encountered an NA value with skipna=False")
return nargminmax(self, "argmax")
def interpolate(
self,
*,
method: InterpolateOptions,
axis: int,
index: Index,
limit,
limit_direction,
limit_area,
copy: bool,
**kwargs,
) -> Self: