17
17
from pandas .core .dtypes .astype import astype_array
18
18
from pandas .core .dtypes .cast import construct_1d_object_array_from_listlike
19
19
from pandas .core .dtypes .common import pandas_dtype
20
- from pandas .core .dtypes .dtypes import PandasDtype
20
+ from pandas .core .dtypes .dtypes import NumpyEADtype
21
21
from pandas .core .dtypes .missing import isna
22
22
23
23
from pandas .core import (
48
48
49
49
# error: Definition of "_concat_same_type" in base class "NDArrayBacked" is
50
50
# incompatible with definition in base class "ExtensionArray"
51
- class PandasArray ( # type: ignore[misc]
51
+ class NumpyExtensionArray ( # type: ignore[misc]
52
52
OpsMixin ,
53
53
NDArrayBackedExtensionArray ,
54
54
ObjectStringArrayMixin ,
@@ -76,19 +76,21 @@ class PandasArray( # type: ignore[misc]
76
76
"""
77
77
78
78
# If you're wondering why pd.Series(cls) doesn't put the array in an
79
- # ExtensionBlock, search for `ABCPandasArray `. We check for
79
+ # ExtensionBlock, search for `ABCNumpyExtensionArray `. We check for
80
80
# that _typ to ensure that users don't unnecessarily use EAs inside
81
81
# pandas internals, which turns off things like block consolidation.
82
82
_typ = "npy_extension"
83
83
__array_priority__ = 1000
84
84
_ndarray : np .ndarray
85
- _dtype : PandasDtype
85
+ _dtype : NumpyEADtype
86
86
_internal_fill_value = np .nan
87
87
88
88
# ------------------------------------------------------------------------
89
89
# Constructors
90
90
91
- def __init__ (self , values : np .ndarray | PandasArray , copy : bool = False ) -> None :
91
+ def __init__ (
92
+ self , values : np .ndarray | NumpyExtensionArray , copy : bool = False
93
+ ) -> None :
92
94
if isinstance (values , type (self )):
93
95
values = values ._ndarray
94
96
if not isinstance (values , np .ndarray ):
@@ -98,19 +100,19 @@ def __init__(self, values: np.ndarray | PandasArray, copy: bool = False) -> None
98
100
99
101
if values .ndim == 0 :
100
102
# Technically we support 2, but do not advertise that fact.
101
- raise ValueError ("PandasArray must be 1-dimensional." )
103
+ raise ValueError ("NumpyExtensionArray must be 1-dimensional." )
102
104
103
105
if copy :
104
106
values = values .copy ()
105
107
106
- dtype = PandasDtype (values .dtype )
108
+ dtype = NumpyEADtype (values .dtype )
107
109
super ().__init__ (values , dtype )
108
110
109
111
@classmethod
110
112
def _from_sequence (
111
113
cls , scalars , * , dtype : Dtype | None = None , copy : bool = False
112
- ) -> PandasArray :
113
- if isinstance (dtype , PandasDtype ):
114
+ ) -> NumpyExtensionArray :
115
+ if isinstance (dtype , NumpyEADtype ):
114
116
dtype = dtype ._dtype
115
117
116
118
# error: Argument "dtype" to "asarray" has incompatible type
@@ -131,14 +133,14 @@ def _from_sequence(
131
133
result = result .copy ()
132
134
return cls (result )
133
135
134
- def _from_backing_data (self , arr : np .ndarray ) -> PandasArray :
136
+ def _from_backing_data (self , arr : np .ndarray ) -> NumpyExtensionArray :
135
137
return type (self )(arr )
136
138
137
139
# ------------------------------------------------------------------------
138
140
# Data
139
141
140
142
@property
141
- def dtype (self ) -> PandasDtype :
143
+ def dtype (self ) -> NumpyEADtype :
142
144
return self ._dtype
143
145
144
146
# ------------------------------------------------------------------------
@@ -151,7 +153,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
151
153
# Lightly modified version of
152
154
# https://numpy.org/doc/stable/reference/generated/numpy.lib.mixins.NDArrayOperatorsMixin.html
153
155
# The primary modification is not boxing scalar return values
154
- # in PandasArray , since pandas' ExtensionArrays are 1-d.
156
+ # in NumpyExtensionArray , since pandas' ExtensionArrays are 1-d.
155
157
out = kwargs .get ("out" , ())
156
158
157
159
result = arraylike .maybe_dispatch_ufunc_to_dunder_op (
@@ -175,10 +177,12 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
175
177
return result
176
178
177
179
# Defer to the implementation of the ufunc on unwrapped values.
178
- inputs = tuple (x ._ndarray if isinstance (x , PandasArray ) else x for x in inputs )
180
+ inputs = tuple (
181
+ x ._ndarray if isinstance (x , NumpyExtensionArray ) else x for x in inputs
182
+ )
179
183
if out :
180
184
kwargs ["out" ] = tuple (
181
- x ._ndarray if isinstance (x , PandasArray ) else x for x in out
185
+ x ._ndarray if isinstance (x , NumpyExtensionArray ) else x for x in out
182
186
)
183
187
result = getattr (ufunc , method )(* inputs , ** kwargs )
184
188
@@ -499,20 +503,20 @@ def to_numpy(
499
503
# ------------------------------------------------------------------------
500
504
# Ops
501
505
502
- def __invert__ (self ) -> PandasArray :
506
+ def __invert__ (self ) -> NumpyExtensionArray :
503
507
return type (self )(~ self ._ndarray )
504
508
505
- def __neg__ (self ) -> PandasArray :
509
+ def __neg__ (self ) -> NumpyExtensionArray :
506
510
return type (self )(- self ._ndarray )
507
511
508
- def __pos__ (self ) -> PandasArray :
512
+ def __pos__ (self ) -> NumpyExtensionArray :
509
513
return type (self )(+ self ._ndarray )
510
514
511
- def __abs__ (self ) -> PandasArray :
515
+ def __abs__ (self ) -> NumpyExtensionArray :
512
516
return type (self )(abs (self ._ndarray ))
513
517
514
518
def _cmp_method (self , other , op ):
515
- if isinstance (other , PandasArray ):
519
+ if isinstance (other , NumpyExtensionArray ):
516
520
other = other ._ndarray
517
521
518
522
other = ops .maybe_prepare_scalar_for_op (other , (len (self ),))
@@ -538,7 +542,7 @@ def _cmp_method(self, other, op):
538
542
539
543
def _wrap_ndarray_result (self , result : np .ndarray ):
540
544
# If we have timedelta64[ns] result, return a TimedeltaArray instead
541
- # of a PandasArray
545
+ # of a NumpyExtensionArray
542
546
if result .dtype .kind == "m" and is_supported_unit (
543
547
get_unit_from_dtype (result .dtype )
544
548
):
0 commit comments