Skip to content

Commit c4fb9dc

Browse files
committed
fix bug
1 parent 8340f09 commit c4fb9dc

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

pandas/core/construction.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
maybe_cast_to_integer_array,
3333
maybe_convert_platform,
3434
maybe_infer_to_datetimelike,
35+
maybe_promote,
3536
)
3637
from pandas.core.dtypes.common import (
3738
ensure_object,
@@ -508,22 +509,21 @@ def ensure_wrapped_if_datetimelike(arr):
508509

509510
def sanitize_masked_array(data: ma.MaskedArray) -> np.ndarray:
510511
"""
511-
Convert numpy MaskedArray to ensure mask is softened.
512+
Convert numpy MaskedArray to ensure mask is softened,
513+
512514
"""
513515
mask = ma.getmaskarray(data)
514516
if mask.any():
515517
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.
523521
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
527527
else:
528528
data = data.copy()
529529
return data

0 commit comments

Comments
 (0)