Skip to content

Commit de2e086

Browse files
jbrockmendeljreback
authored andcommitted
REF: simplify maybe_promote (#28899)
1 parent 0c4404b commit de2e086

File tree

1 file changed

+3
-46
lines changed

1 file changed

+3
-46
lines changed

pandas/core/dtypes/cast.py

+3-46
Original file line numberDiff line numberDiff line change
@@ -423,57 +423,14 @@ def maybe_promote(dtype, fill_value=np.nan):
423423
if issubclass(dtype.type, np.bool_):
424424
dtype = np.dtype(np.object_)
425425
elif issubclass(dtype.type, np.integer):
426-
# upcast to prevent overflow
427-
mst = np.min_scalar_type(fill_value)
428-
if mst > dtype:
429-
# np.dtype ordering considers:
430-
# int[n] < int[2*n]
431-
# uint[n] < uint[2*n]
432-
# u?int[n] < object_
433-
dtype = mst
434-
435-
elif np.can_cast(fill_value, dtype):
436-
pass
437-
438-
elif dtype.kind == "u" and mst.kind == "i":
426+
if not np.can_cast(fill_value, dtype):
427+
# upcast to prevent overflow
428+
mst = np.min_scalar_type(fill_value)
439429
dtype = np.promote_types(dtype, mst)
440430
if dtype.kind == "f":
441431
# Case where we disagree with numpy
442432
dtype = np.dtype(np.object_)
443433

444-
elif dtype.kind == "i" and mst.kind == "u":
445-
446-
if fill_value > np.iinfo(np.int64).max:
447-
# object is the only way to represent fill_value and keep
448-
# the range allowed by the given dtype
449-
dtype = np.dtype(np.object_)
450-
451-
elif mst.itemsize < dtype.itemsize:
452-
pass
453-
454-
elif dtype.itemsize == mst.itemsize:
455-
# We never cast signed to unsigned because that loses
456-
# parts of the original range, so find the smallest signed
457-
# integer that can hold all of `mst`.
458-
ndt = {
459-
np.int64: np.object_,
460-
np.int32: np.int64,
461-
np.int16: np.int32,
462-
np.int8: np.int16,
463-
}[dtype.type]
464-
dtype = np.dtype(ndt)
465-
466-
else:
467-
# bump to signed integer dtype that holds all of `mst` range
468-
# Note: we have to use itemsize because some (windows)
469-
# builds don't satisfiy e.g. np.uint32 == np.uint32
470-
ndt = {
471-
4: np.int64,
472-
2: np.int32,
473-
1: np.int16, # TODO: Test for this case
474-
}[mst.itemsize]
475-
dtype = np.dtype(ndt)
476-
477434
fill_value = dtype.type(fill_value)
478435

479436
elif issubclass(dtype.type, np.floating):

0 commit comments

Comments
 (0)