Skip to content

Commit 08d349e

Browse files
authored
REF: dispatch maybe_promote to infer_dtype_from_scalar for td64 dtype (#39781)
1 parent 6716589 commit 08d349e

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

pandas/core/dtypes/cast.py

+6-19
Original file line numberDiff line numberDiff line change
@@ -609,24 +609,10 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
609609
return np.dtype(object), fill_value
610610

611611
elif issubclass(dtype.type, np.timedelta64):
612-
if (
613-
is_integer(fill_value)
614-
or is_float(fill_value)
615-
or isinstance(fill_value, str)
616-
):
617-
# TODO: What about str that can be a timedelta?
618-
dtype = np.dtype(np.object_)
619-
else:
620-
try:
621-
fv = Timedelta(fill_value)
622-
except ValueError:
623-
dtype = np.dtype(np.object_)
624-
else:
625-
if fv is NaT:
626-
# NaT has no `to_timedelta64` method
627-
fill_value = np.timedelta64("NaT", "ns")
628-
else:
629-
fill_value = fv.to_timedelta64()
612+
inferred, fv = infer_dtype_from_scalar(fill_value, pandas_dtype=True)
613+
if inferred == dtype:
614+
return dtype, fv
615+
return np.dtype(object), fill_value
630616

631617
elif is_float(fill_value):
632618
if issubclass(dtype.type, np.bool_):
@@ -782,11 +768,12 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
782768

783769
elif isinstance(val, (np.timedelta64, timedelta)):
784770
try:
785-
val = Timedelta(val).value
771+
val = Timedelta(val)
786772
except (OutOfBoundsTimedelta, OverflowError):
787773
dtype = np.dtype(object)
788774
else:
789775
dtype = np.dtype("m8[ns]")
776+
val = np.timedelta64(val.value, "ns")
790777

791778
elif is_bool(val):
792779
dtype = np.dtype(np.bool_)

0 commit comments

Comments
 (0)