Skip to content

Commit 19d1c30

Browse files
jbrockmendelproost
authored andcommitted
CLN: simplify maybe_promote in float and complex cases (pandas-dev#28913)
1 parent 60e527e commit 19d1c30

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

pandas/core/dtypes/cast.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,8 @@ def maybe_promote(dtype, fill_value=np.nan):
405405
dtype = np.min_scalar_type(fill_value)
406406

407407
elif dtype.kind == "c":
408-
if not np.can_cast(fill_value, dtype):
409-
if np.can_cast(fill_value, np.dtype("c16")):
410-
dtype = np.dtype(np.complex128)
411-
else:
412-
dtype = np.dtype(np.object_)
408+
mst = np.min_scalar_type(fill_value)
409+
dtype = np.promote_types(dtype, mst)
413410

414411
if dtype.kind == "c" and not np.isnan(fill_value):
415412
fill_value = dtype.type(fill_value)
@@ -447,16 +444,8 @@ def maybe_promote(dtype, fill_value=np.nan):
447444
if issubclass(dtype.type, np.bool_):
448445
dtype = np.dtype(np.object_)
449446
elif issubclass(dtype.type, (np.integer, np.floating)):
450-
c8 = np.dtype(np.complex64)
451-
info = np.finfo(dtype) if dtype.kind == "f" else np.iinfo(dtype)
452-
if (
453-
np.can_cast(fill_value, c8)
454-
and np.can_cast(info.min, c8)
455-
and np.can_cast(info.max, c8)
456-
):
457-
dtype = np.dtype(np.complex64)
458-
else:
459-
dtype = np.dtype(np.complex128)
447+
mst = np.min_scalar_type(fill_value)
448+
dtype = np.promote_types(dtype, mst)
460449

461450
elif dtype.kind == "c":
462451
mst = np.min_scalar_type(fill_value)

0 commit comments

Comments
 (0)