File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 4747 maybe_promote ,
4848)
4949from pandas .core .dtypes .common import (
50+ is_float_dtype ,
51+ is_integer_dtype ,
5052 is_list_like ,
5153 is_object_dtype ,
5254 is_string_dtype ,
@@ -503,11 +505,29 @@ def sanitize_masked_array(data: ma.MaskedArray) -> np.ndarray:
503505 Convert numpy MaskedArray to ensure mask is softened.
504506 """
505507 mask = ma .getmaskarray (data )
508+ original = data
509+ original_dtype = data .dtype
506510 if mask .any ():
507511 dtype , fill_value = maybe_promote (data .dtype , np .nan )
508512 dtype = cast (np .dtype , dtype )
509513 data = ma .asarray (data .astype (dtype , copy = True ))
510514 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 )
511531 data [mask ] = fill_value
512532 else :
513533 data = data .copy ()
You can’t perform that action at this time.
0 commit comments