|
32 | 32 | maybe_cast_to_integer_array,
|
33 | 33 | maybe_convert_platform,
|
34 | 34 | maybe_infer_to_datetimelike,
|
| 35 | + maybe_promote, |
35 | 36 | )
|
36 | 37 | from pandas.core.dtypes.common import (
|
37 | 38 | ensure_object,
|
@@ -508,22 +509,21 @@ def ensure_wrapped_if_datetimelike(arr):
|
508 | 509 |
|
509 | 510 | def sanitize_masked_array(data: ma.MaskedArray) -> np.ndarray:
|
510 | 511 | """
|
511 |
| - Convert numpy MaskedArray to ensure mask is softened. |
| 512 | + Convert numpy MaskedArray to ensure mask is softened, |
| 513 | +
|
512 | 514 | """
|
513 | 515 | mask = ma.getmaskarray(data)
|
514 | 516 | if mask.any():
|
515 | 517 | dtype = cast(np.dtype, data.dtype)
|
516 |
| - data = ma.asarray(data.astype(dtype, copy=True)) |
517 |
| - data.soften_mask() # set hardmask False if it was True |
518 |
| - |
519 |
| - if np.issubdtype(dtype, np.integer): |
520 |
| - fill_value: int | float | None = np.iinfo(dtype).min |
521 |
| - elif np.issubdtype(dtype, np.floating): |
522 |
| - fill_value = np.nan |
| 518 | + if isinstance(dtype, ExtensionDtype) and dtype.name.startswith("Masked"): |
| 519 | + data = ma.asarray(data.astype(dtype, copy=True)) |
| 520 | + data.soften_mask() # If the data is a Masked EA, directly soften the mask. |
523 | 521 | else:
|
524 |
| - fill_value = None |
525 |
| - |
526 |
| - data[mask] = fill_value |
| 522 | + dtype, fill_value = maybe_promote(data.dtype, np.nan) |
| 523 | + dtype = cast(np.dtype, dtype) |
| 524 | + data = ma.asarray(data.astype(dtype, copy=True)) |
| 525 | + data.soften_mask() # set hardmask False if it was True |
| 526 | + data[mask] = fill_value |
527 | 527 | else:
|
528 | 528 | data = data.copy()
|
529 | 529 | return data
|
|
0 commit comments