6
6
Any ,
7
7
Literal ,
8
8
Sequence ,
9
- TypeVar ,
10
9
cast ,
11
10
overload ,
12
11
)
23
22
PositionalIndexer2D ,
24
23
PositionalIndexerTuple ,
25
24
ScalarIndexer ,
25
+ Self ,
26
26
SequenceIndexer ,
27
27
Shape ,
28
28
TakeIndexer ,
29
29
npt ,
30
- type_t ,
31
30
)
32
31
from pandas .errors import AbstractMethodError
33
32
from pandas .util ._decorators import doc
61
60
from pandas .core .indexers import check_array_indexer
62
61
from pandas .core .sorting import nargminmax
63
62
64
- NDArrayBackedExtensionArrayT = TypeVar (
65
- "NDArrayBackedExtensionArrayT" , bound = "NDArrayBackedExtensionArray"
66
- )
67
-
68
63
if TYPE_CHECKING :
69
64
from pandas ._typing import (
70
65
NumpySorter ,
@@ -153,13 +148,13 @@ def view(self, dtype: Dtype | None = None) -> ArrayLike:
153
148
return arr .view (dtype = dtype ) # type: ignore[arg-type]
154
149
155
150
def take (
156
- self : NDArrayBackedExtensionArrayT ,
151
+ self ,
157
152
indices : TakeIndexer ,
158
153
* ,
159
154
allow_fill : bool = False ,
160
155
fill_value : Any = None ,
161
156
axis : AxisInt = 0 ,
162
- ) -> NDArrayBackedExtensionArrayT :
157
+ ) -> Self :
163
158
if allow_fill :
164
159
fill_value = self ._validate_scalar (fill_value )
165
160
@@ -218,17 +213,17 @@ def argmax(self, axis: AxisInt = 0, skipna: bool = True): # type: ignore[overri
218
213
raise NotImplementedError
219
214
return nargminmax (self , "argmax" , axis = axis )
220
215
221
- def unique (self : NDArrayBackedExtensionArrayT ) -> NDArrayBackedExtensionArrayT :
216
+ def unique (self ) -> Self :
222
217
new_data = unique (self ._ndarray )
223
218
return self ._from_backing_data (new_data )
224
219
225
220
@classmethod
226
221
@doc (ExtensionArray ._concat_same_type )
227
222
def _concat_same_type (
228
- cls : type [ NDArrayBackedExtensionArrayT ] ,
229
- to_concat : Sequence [NDArrayBackedExtensionArrayT ],
223
+ cls ,
224
+ to_concat : Sequence [Self ],
230
225
axis : AxisInt = 0 ,
231
- ) -> NDArrayBackedExtensionArrayT :
226
+ ) -> Self :
232
227
dtypes = {str (x .dtype ) for x in to_concat }
233
228
if len (dtypes ) != 1 :
234
229
raise ValueError ("to_concat must have the same dtype (tz)" , dtypes )
@@ -268,15 +263,15 @@ def __getitem__(self, key: ScalarIndexer) -> Any:
268
263
269
264
@overload
270
265
def __getitem__ (
271
- self : NDArrayBackedExtensionArrayT ,
266
+ self ,
272
267
key : SequenceIndexer | PositionalIndexerTuple ,
273
- ) -> NDArrayBackedExtensionArrayT :
268
+ ) -> Self :
274
269
...
275
270
276
271
def __getitem__ (
277
- self : NDArrayBackedExtensionArrayT ,
272
+ self ,
278
273
key : PositionalIndexer2D ,
279
- ) -> NDArrayBackedExtensionArrayT | Any :
274
+ ) -> Self | Any :
280
275
if lib .is_integer (key ):
281
276
# fast-path
282
277
result = self ._ndarray [key ]
@@ -303,9 +298,7 @@ def _fill_mask_inplace(
303
298
func (self ._ndarray .T , limit = limit , mask = mask .T )
304
299
305
300
@doc (ExtensionArray .fillna )
306
- def fillna (
307
- self : NDArrayBackedExtensionArrayT , value = None , method = None , limit = None
308
- ) -> NDArrayBackedExtensionArrayT :
301
+ def fillna (self , value = None , method = None , limit = None ) -> Self :
309
302
value , method = validate_fillna_kwargs (
310
303
value , method , validate_scalar_dict_value = False
311
304
)
@@ -369,9 +362,7 @@ def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
369
362
370
363
np .putmask (self ._ndarray , mask , value )
371
364
372
- def _where (
373
- self : NDArrayBackedExtensionArrayT , mask : npt .NDArray [np .bool_ ], value
374
- ) -> NDArrayBackedExtensionArrayT :
365
+ def _where (self : Self , mask : npt .NDArray [np .bool_ ], value ) -> Self :
375
366
"""
376
367
Analogue to np.where(mask, self, value)
377
368
@@ -393,9 +384,7 @@ def _where(
393
384
# ------------------------------------------------------------------------
394
385
# Index compat methods
395
386
396
- def insert (
397
- self : NDArrayBackedExtensionArrayT , loc : int , item
398
- ) -> NDArrayBackedExtensionArrayT :
387
+ def insert (self , loc : int , item ) -> Self :
399
388
"""
400
389
Make new ExtensionArray inserting new item at location. Follows
401
390
Python list.append semantics for negative values.
@@ -461,10 +450,10 @@ def value_counts(self, dropna: bool = True) -> Series:
461
450
return Series (result ._values , index = index , name = result .name )
462
451
463
452
def _quantile (
464
- self : NDArrayBackedExtensionArrayT ,
453
+ self ,
465
454
qs : npt .NDArray [np .float64 ],
466
455
interpolation : str ,
467
- ) -> NDArrayBackedExtensionArrayT :
456
+ ) -> Self :
468
457
# TODO: disable for Categorical if not ordered?
469
458
470
459
mask = np .asarray (self .isna ())
@@ -488,9 +477,7 @@ def _cast_quantile_result(self, res_values: np.ndarray) -> np.ndarray:
488
477
# numpy-like methods
489
478
490
479
@classmethod
491
- def _empty (
492
- cls : type_t [NDArrayBackedExtensionArrayT ], shape : Shape , dtype : ExtensionDtype
493
- ) -> NDArrayBackedExtensionArrayT :
480
+ def _empty (cls , shape : Shape , dtype : ExtensionDtype ) -> Self :
494
481
"""
495
482
Analogous to np.empty(shape, dtype=dtype)
496
483
0 commit comments