99 Callable ,
1010 Hashable ,
1111 Literal ,
12- TypeVar ,
1312)
1413
1514import numpy as np
9392 AxisInt ,
9493 DtypeObj ,
9594 QuantileInterpolation ,
95+ Self ,
9696 npt ,
9797 )
98- T = TypeVar ("T" , bound = "BaseArrayManager" )
9998
10099
101100class BaseArrayManager (DataManager ):
@@ -131,7 +130,7 @@ def __init__(
131130 ) -> None :
132131 raise NotImplementedError
133132
134- def make_empty (self : T , axes = None ) -> T :
133+ def make_empty (self , axes = None ) -> Self :
135134 """Return an empty ArrayManager with the items axis of len 0 (no columns)"""
136135 if axes is None :
137136 axes = [self .axes [1 :], Index ([])]
@@ -195,11 +194,11 @@ def __repr__(self) -> str:
195194 return output
196195
197196 def apply (
198- self : T ,
197+ self ,
199198 f ,
200199 align_keys : list [str ] | None = None ,
201200 ** kwargs ,
202- ) -> T :
201+ ) -> Self :
203202 """
204203 Iterate over the arrays, collect and create a new ArrayManager.
205204
@@ -257,8 +256,8 @@ def apply(
257256 return type (self )(result_arrays , new_axes ) # type: ignore[arg-type]
258257
259258 def apply_with_block (
260- self : T , f , align_keys = None , swap_axis : bool = True , ** kwargs
261- ) -> T :
259+ self , f , align_keys = None , swap_axis : bool = True , ** kwargs
260+ ) -> Self :
262261 # switch axis to follow BlockManager logic
263262 if swap_axis and "axis" in kwargs and self .ndim == 2 :
264263 kwargs ["axis" ] = 1 if kwargs ["axis" ] == 0 else 0
@@ -311,7 +310,7 @@ def apply_with_block(
311310
312311 return type (self )(result_arrays , self ._axes )
313312
314- def where (self : T , other , cond , align : bool ) -> T :
313+ def where (self , other , cond , align : bool ) -> Self :
315314 if align :
316315 align_keys = ["other" , "cond" ]
317316 else :
@@ -325,13 +324,13 @@ def where(self: T, other, cond, align: bool) -> T:
325324 cond = cond ,
326325 )
327326
328- def round (self : T , decimals : int , using_cow : bool = False ) -> T :
327+ def round (self , decimals : int , using_cow : bool = False ) -> Self :
329328 return self .apply_with_block ("round" , decimals = decimals , using_cow = using_cow )
330329
331- def setitem (self : T , indexer , value ) -> T :
330+ def setitem (self , indexer , value ) -> Self :
332331 return self .apply_with_block ("setitem" , indexer = indexer , value = value )
333332
334- def putmask (self : T , mask , new , align : bool = True ) -> T :
333+ def putmask (self , mask , new , align : bool = True ) -> Self :
335334 if align :
336335 align_keys = ["new" , "mask" ]
337336 else :
@@ -345,14 +344,14 @@ def putmask(self: T, mask, new, align: bool = True) -> T:
345344 new = new ,
346345 )
347346
348- def diff (self : T , n : int , axis : AxisInt ) -> T :
347+ def diff (self , n : int , axis : AxisInt ) -> Self :
349348 assert self .ndim == 2 and axis == 0 # caller ensures
350349 return self .apply (algos .diff , n = n , axis = axis )
351350
352- def interpolate (self : T , ** kwargs ) -> T :
351+ def interpolate (self , ** kwargs ) -> Self :
353352 return self .apply_with_block ("interpolate" , swap_axis = False , ** kwargs )
354353
355- def shift (self : T , periods : int , axis : AxisInt , fill_value ) -> T :
354+ def shift (self , periods : int , axis : AxisInt , fill_value ) -> Self :
356355 if fill_value is lib .no_default :
357356 fill_value = None
358357
@@ -364,7 +363,7 @@ def shift(self: T, periods: int, axis: AxisInt, fill_value) -> T:
364363 "shift" , periods = periods , axis = axis , fill_value = fill_value
365364 )
366365
367- def fillna (self : T , value , limit , inplace : bool , downcast ) -> T :
366+ def fillna (self , value , limit , inplace : bool , downcast ) -> Self :
368367 if limit is not None :
369368 # Do this validation even if we go through one of the no-op paths
370369 limit = libalgos .validate_limit (None , limit = limit )
@@ -373,13 +372,13 @@ def fillna(self: T, value, limit, inplace: bool, downcast) -> T:
373372 "fillna" , value = value , limit = limit , inplace = inplace , downcast = downcast
374373 )
375374
376- def astype (self : T , dtype , copy : bool | None = False , errors : str = "raise" ) -> T :
375+ def astype (self , dtype , copy : bool | None = False , errors : str = "raise" ) -> Self :
377376 if copy is None :
378377 copy = True
379378
380379 return self .apply (astype_array_safe , dtype = dtype , copy = copy , errors = errors )
381380
382- def convert (self : T , copy : bool | None ) -> T :
381+ def convert (self , copy : bool | None ) -> Self :
383382 if copy is None :
384383 copy = True
385384
@@ -402,10 +401,10 @@ def _convert(arr):
402401
403402 return self .apply (_convert )
404403
405- def replace_regex (self : T , ** kwargs ) -> T :
404+ def replace_regex (self , ** kwargs ) -> Self :
406405 return self .apply_with_block ("_replace_regex" , ** kwargs )
407406
408- def replace (self : T , to_replace , value , inplace : bool ) -> T :
407+ def replace (self , to_replace , value , inplace : bool ) -> Self :
409408 inplace = validate_bool_kwarg (inplace , "inplace" )
410409 assert np .ndim (value ) == 0 , value
411410 # TODO "replace" is right now implemented on the blocks, we should move
@@ -415,12 +414,12 @@ def replace(self: T, to_replace, value, inplace: bool) -> T:
415414 )
416415
417416 def replace_list (
418- self : T ,
417+ self ,
419418 src_list : list [Any ],
420419 dest_list : list [Any ],
421420 inplace : bool = False ,
422421 regex : bool = False ,
423- ) -> T :
422+ ) -> Self :
424423 """do a list replace"""
425424 inplace = validate_bool_kwarg (inplace , "inplace" )
426425
@@ -432,7 +431,7 @@ def replace_list(
432431 regex = regex ,
433432 )
434433
435- def to_native_types (self : T , ** kwargs ) -> T :
434+ def to_native_types (self , ** kwargs ) -> Self :
436435 return self .apply (to_native_types , ** kwargs )
437436
438437 @property
@@ -458,7 +457,7 @@ def is_view(self) -> bool:
458457 def is_single_block (self ) -> bool :
459458 return len (self .arrays ) == 1
460459
461- def _get_data_subset (self : T , predicate : Callable ) -> T :
460+ def _get_data_subset (self , predicate : Callable ) -> Self :
462461 indices = [i for i , arr in enumerate (self .arrays ) if predicate (arr )]
463462 arrays = [self .arrays [i ] for i in indices ]
464463 # TODO copy?
@@ -469,7 +468,7 @@ def _get_data_subset(self: T, predicate: Callable) -> T:
469468 new_axes = [self ._axes [0 ], new_cols ]
470469 return type (self )(arrays , new_axes , verify_integrity = False )
471470
472- def get_bool_data (self : T , copy : bool = False ) -> T :
471+ def get_bool_data (self , copy : bool = False ) -> Self :
473472 """
474473 Select columns that are bool-dtype and object-dtype columns that are all-bool.
475474
@@ -480,7 +479,7 @@ def get_bool_data(self: T, copy: bool = False) -> T:
480479 """
481480 return self ._get_data_subset (lambda x : x .dtype == np .dtype (bool ))
482481
483- def get_numeric_data (self : T , copy : bool = False ) -> T :
482+ def get_numeric_data (self , copy : bool = False ) -> Self :
484483 """
485484 Select columns that have a numeric dtype.
486485
@@ -494,7 +493,7 @@ def get_numeric_data(self: T, copy: bool = False) -> T:
494493 or getattr (arr .dtype , "_is_numeric" , False )
495494 )
496495
497- def copy (self : T , deep : bool | Literal ["all" ] | None = True ) -> T :
496+ def copy (self , deep : bool | Literal ["all" ] | None = True ) -> Self :
498497 """
499498 Make deep or shallow copy of ArrayManager
500499
@@ -531,7 +530,7 @@ def copy_func(ax):
531530 return type (self )(new_arrays , new_axes , verify_integrity = False )
532531
533532 def reindex_indexer (
534- self : T ,
533+ self ,
535534 new_axis ,
536535 indexer ,
537536 axis : AxisInt ,
@@ -542,7 +541,7 @@ def reindex_indexer(
542541 only_slice : bool = False ,
543542 # ArrayManager specific keywords
544543 use_na_proxy : bool = False ,
545- ) -> T :
544+ ) -> Self :
546545 axis = self ._normalize_axis (axis )
547546 return self ._reindex_indexer (
548547 new_axis ,
@@ -555,15 +554,15 @@ def reindex_indexer(
555554 )
556555
557556 def _reindex_indexer (
558- self : T ,
557+ self ,
559558 new_axis ,
560559 indexer : npt .NDArray [np .intp ] | None ,
561560 axis : AxisInt ,
562561 fill_value = None ,
563562 allow_dups : bool = False ,
564563 copy : bool | None = True ,
565564 use_na_proxy : bool = False ,
566- ) -> T :
565+ ) -> Self :
567566 """
568567 Parameters
569568 ----------
@@ -634,11 +633,11 @@ def _reindex_indexer(
634633 return type (self )(new_arrays , new_axes , verify_integrity = False )
635634
636635 def take (
637- self : T ,
636+ self ,
638637 indexer : npt .NDArray [np .intp ],
639638 axis : AxisInt = 1 ,
640639 verify : bool = True ,
641- ) -> T :
640+ ) -> Self :
642641 """
643642 Take items along any axis.
644643 """
@@ -926,7 +925,7 @@ def idelete(self, indexer) -> ArrayManager:
926925 # --------------------------------------------------------------------
927926 # Array-wise Operation
928927
929- def grouped_reduce (self : T , func : Callable ) -> T :
928+ def grouped_reduce (self , func : Callable ) -> Self :
930929 """
931930 Apply grouped reduction function columnwise, returning a new ArrayManager.
932931
@@ -965,7 +964,7 @@ def grouped_reduce(self: T, func: Callable) -> T:
965964 # expected "List[Union[ndarray, ExtensionArray]]"
966965 return type (self )(result_arrays , [index , columns ]) # type: ignore[arg-type]
967966
968- def reduce (self : T , func : Callable ) -> T :
967+ def reduce (self , func : Callable ) -> Self :
969968 """
970969 Apply reduction function column-wise, returning a single-row ArrayManager.
971970
@@ -1199,8 +1198,9 @@ def make_empty(self, axes=None) -> SingleArrayManager:
11991198 def from_array (cls , array , index ) -> SingleArrayManager :
12001199 return cls ([array ], [index ])
12011200
1201+ # error: Cannot override writeable attribute with read-only property
12021202 @property
1203- def axes (self ):
1203+ def axes (self ) -> list [ Index ]: # type: ignore[override]
12041204 return self ._axes
12051205
12061206 @property
@@ -1254,7 +1254,8 @@ def getitem_mgr(self, indexer) -> SingleArrayManager:
12541254 new_index = self .index [indexer ]
12551255 return type (self )([new_array ], [new_index ])
12561256
1257- def apply (self , func , ** kwargs ):
1257+ # error: Signature of "apply" incompatible with supertype "BaseArrayManager"
1258+ def apply (self , func , ** kwargs ) -> Self : # type: ignore[override]
12581259 if callable (func ):
12591260 new_array = func (self .array , ** kwargs )
12601261 else :
0 commit comments