1
1
import numbers
2
- from typing import TYPE_CHECKING , Any , List , Tuple , Type , Union
2
+ from typing import TYPE_CHECKING , List , Tuple , Type , Union
3
3
import warnings
4
4
5
5
import numpy as np
6
6
7
7
from pandas ._libs import lib , missing as libmissing
8
+ from pandas ._typing import ArrayLike
8
9
from pandas .compat import set_function_name
9
10
from pandas .compat .numpy import function as nv
10
11
@@ -281,20 +282,15 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
281
282
if not mask .ndim == 1 :
282
283
raise ValueError ("mask must be a 1D array" )
283
284
284
- if copy :
285
- values = values .copy ()
286
- mask = mask .copy ()
287
-
288
- self ._data = values
289
- self ._mask = mask
290
285
self ._dtype = BooleanDtype ()
286
+ super ().__init__ (values , mask , copy = copy )
291
287
292
288
@property
293
- def dtype (self ):
289
+ def dtype (self ) -> BooleanDtype :
294
290
return self ._dtype
295
291
296
292
@classmethod
297
- def _from_sequence (cls , scalars , dtype = None , copy : bool = False ):
293
+ def _from_sequence (cls , scalars , dtype = None , copy : bool = False ) -> "BooleanArray" :
298
294
if dtype :
299
295
assert dtype == "boolean"
300
296
values , mask = coerce_to_array (scalars , copy = copy )
@@ -303,7 +299,7 @@ def _from_sequence(cls, scalars, dtype=None, copy: bool = False):
303
299
@classmethod
304
300
def _from_sequence_of_strings (
305
301
cls , strings : List [str ], dtype = None , copy : bool = False
306
- ):
302
+ ) -> "BooleanArray" :
307
303
def map_string (s ):
308
304
if isna (s ):
309
305
return s
@@ -317,18 +313,18 @@ def map_string(s):
317
313
scalars = [map_string (x ) for x in strings ]
318
314
return cls ._from_sequence (scalars , dtype , copy )
319
315
320
- def _values_for_factorize (self ) -> Tuple [np .ndarray , Any ]:
316
+ def _values_for_factorize (self ) -> Tuple [np .ndarray , int ]:
321
317
data = self ._data .astype ("int8" )
322
318
data [self ._mask ] = - 1
323
319
return data , - 1
324
320
325
321
@classmethod
326
- def _from_factorized (cls , values , original : "BooleanArray" ):
322
+ def _from_factorized (cls , values , original : "BooleanArray" ) -> "BooleanArray" :
327
323
return cls ._from_sequence (values , dtype = original .dtype )
328
324
329
325
_HANDLED_TYPES = (np .ndarray , numbers .Number , bool , np .bool_ )
330
326
331
- def __array_ufunc__ (self , ufunc , method , * inputs , ** kwargs ):
327
+ def __array_ufunc__ (self , ufunc , method : str , * inputs , ** kwargs ):
332
328
# For BooleanArray inputs, we apply the ufunc to ._data
333
329
# and mask the result.
334
330
if method == "reduce" :
@@ -373,7 +369,7 @@ def reconstruct(x):
373
369
else :
374
370
return reconstruct (result )
375
371
376
- def __setitem__ (self , key , value ):
372
+ def __setitem__ (self , key , value ) -> None :
377
373
_is_scalar = is_scalar (value )
378
374
if _is_scalar :
379
375
value = [value ]
@@ -387,7 +383,7 @@ def __setitem__(self, key, value):
387
383
self ._data [key ] = value
388
384
self ._mask [key ] = mask
389
385
390
- def astype (self , dtype , copy = True ):
386
+ def astype (self , dtype , copy : bool = True ) -> ArrayLike :
391
387
"""
392
388
Cast to a NumPy array or ExtensionArray with 'dtype'.
393
389
@@ -402,8 +398,8 @@ def astype(self, dtype, copy=True):
402
398
403
399
Returns
404
400
-------
405
- array : ndarray or ExtensionArray
406
- NumPy ndarray, BooleanArray or IntergerArray with 'dtype' for its dtype.
401
+ ndarray or ExtensionArray
402
+ NumPy ndarray, BooleanArray or IntegerArray with 'dtype' for its dtype.
407
403
408
404
Raises
409
405
------
@@ -693,7 +689,7 @@ def cmp_method(self, other):
693
689
name = f"__{ op .__name__ } "
694
690
return set_function_name (cmp_method , name , cls )
695
691
696
- def _reduce (self , name , skipna = True , ** kwargs ):
692
+ def _reduce (self , name : str , skipna : bool = True , ** kwargs ):
697
693
698
694
if name in {"any" , "all" }:
699
695
return getattr (self , name )(skipna = skipna , ** kwargs )
@@ -722,7 +718,7 @@ def _reduce(self, name, skipna=True, **kwargs):
722
718
723
719
return result
724
720
725
- def _maybe_mask_result (self , result , mask , other , op_name ):
721
+ def _maybe_mask_result (self , result , mask , other , op_name : str ):
726
722
"""
727
723
Parameters
728
724
----------
0 commit comments