Skip to content

REF: dispatch maybe_promote to infer_dtype_from_scalar for td64 dtype #39781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 16, 2021
Merged
25 changes: 6 additions & 19 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,24 +590,10 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
return np.dtype(object), fill_value

elif issubclass(dtype.type, np.timedelta64):
if (
is_integer(fill_value)
or is_float(fill_value)
or isinstance(fill_value, str)
):
# TODO: What about str that can be a timedelta?
dtype = np.dtype(np.object_)
else:
try:
fv = Timedelta(fill_value)
except ValueError:
dtype = np.dtype(np.object_)
else:
if fv is NaT:
# NaT has no `to_timedelta64` method
fill_value = np.timedelta64("NaT", "ns")
else:
fill_value = fv.to_timedelta64()
inferred, fv = infer_dtype_from_scalar(fill_value, pandas_dtype=True)
if inferred == dtype:
return dtype, fv
return np.dtype(object), fill_value

elif is_float(fill_value):
if issubclass(dtype.type, np.bool_):
Expand Down Expand Up @@ -763,11 +749,12 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,

elif isinstance(val, (np.timedelta64, timedelta)):
try:
val = Timedelta(val).value
val = Timedelta(val)
except (OutOfBoundsTimedelta, OverflowError):
dtype = np.dtype(object)
else:
dtype = np.dtype("m8[ns]")
val = np.timedelta64(val.value, "ns")

elif is_bool(val):
dtype = np.dtype(np.bool_)
Expand Down