File tree 1 file changed +20
-0
lines changed
1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 47
47
maybe_promote ,
48
48
)
49
49
from pandas .core .dtypes .common import (
50
+ is_float_dtype ,
51
+ is_integer_dtype ,
50
52
is_list_like ,
51
53
is_object_dtype ,
52
54
is_string_dtype ,
@@ -503,11 +505,29 @@ def sanitize_masked_array(data: ma.MaskedArray) -> np.ndarray:
503
505
Convert numpy MaskedArray to ensure mask is softened.
504
506
"""
505
507
mask = ma .getmaskarray (data )
508
+ original = data
509
+ original_dtype = data .dtype
506
510
if mask .any ():
507
511
dtype , fill_value = maybe_promote (data .dtype , np .nan )
508
512
dtype = cast (np .dtype , dtype )
509
513
data = ma .asarray (data .astype (dtype , copy = True ))
510
514
data .soften_mask () # set hardmask False if it was True
515
+ if not mask .all ():
516
+ idx = np .unravel_index (np .nanargmax (data , axis = None ), data .shape )
517
+ if not mask [idx ] and int (data [idx ]) != original [idx ]:
518
+ if (
519
+ is_integer_dtype (original_dtype )
520
+ and is_float_dtype (data .dtype )
521
+ and len (data ) > 0
522
+ ):
523
+ inferred_type = lib .infer_dtype (original , skipna = True )
524
+ if (
525
+ inferred_type not in ["floating" , "mixed-integer-float" ]
526
+ and not mask .any ()
527
+ ):
528
+ data = np .array (original , dtype = dtype , copy = False )
529
+ else :
530
+ data = np .array (original , dtype = "object" , copy = False )
511
531
data [mask ] = fill_value
512
532
else :
513
533
data = data .copy ()
You can’t perform that action at this time.
0 commit comments