Skip to content

Commit a24afa8

Browse files
committed
BUG: Fix inconsistency when constructing a Series with large integers in a int64 masked array
- Refered code from PR#50757 similar issue for non masked ints
1 parent 98e1d2f commit a24afa8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pandas/core/construction.py

+20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
maybe_promote,
4848
)
4949
from 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()

0 commit comments

Comments
 (0)