Skip to content
18 changes: 13 additions & 5 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,22 @@ def maybe_promote(dtype, fill_value=np.nan):
dtype = np.dtype(np.object_)
fill_value = np.nan

if dtype == np.object_ or dtype.kind in ["U", "S"]:
# We treat string-like dtypes as object, and _always_ fill
# with np.nan
fill_value = np.nan
dtype = np.dtype(np.object_)

# returns tuple of (dtype, fill_value)
if issubclass(dtype.type, np.datetime64):
try:
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
except (TypeError, ValueError):
if isinstance(fill_value, datetime) and fill_value.tzinfo is not None:
# Trying to insert tzaware into tznaive, have to cast to object
dtype = np.dtype(np.object_)
else:
try:
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
except (TypeError, ValueError):
dtype = np.dtype(np.object_)
elif issubclass(dtype.type, np.timedelta64):
try:
fv = tslibs.Timedelta(fill_value)
Expand Down Expand Up @@ -417,8 +427,6 @@ def maybe_promote(dtype, fill_value=np.nan):
# in case we have a string that looked like a number
if is_extension_array_dtype(dtype):
pass
elif is_datetime64tz_dtype(dtype):
pass
elif issubclass(np.dtype(dtype).type, (bytes, str)):
dtype = np.object_

Expand Down
11 changes: 0 additions & 11 deletions pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,6 @@ def test_maybe_promote_any_numpy_dtype_with_datetimetz(
fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture)
boxed, box_dtype = box # read from parametrized fixture

if dtype.kind == "M" and not boxed:
pytest.xfail("Comes back as M8 instead of object")

fill_value = pd.Series([fill_value], dtype=fill_dtype)[0]

# filling any numpy dtype with datetimetz casts to object
Expand Down Expand Up @@ -847,11 +844,6 @@ def test_maybe_promote_string_with_any(string_dtype, any_numpy_dtype_reduced, bo
fill_dtype = np.dtype(any_numpy_dtype_reduced)
boxed, box_dtype = box # read from parametrized fixture

if boxed and box_dtype is None and fill_dtype.kind == "m":
pytest.xfail("wrong missing value marker")
if boxed and box_dtype is None and fill_dtype.kind == "M":
pytest.xfail("wrong missing value marker")

# create array of given dtype; casts "1" to correct dtype
fill_value = np.array([1], dtype=fill_dtype)[0]

Expand Down Expand Up @@ -914,9 +906,6 @@ def test_maybe_promote_object_with_any(object_dtype, any_numpy_dtype_reduced, bo
fill_dtype = np.dtype(any_numpy_dtype_reduced)
boxed, box_dtype = box # read from parametrized fixture

if boxed and box_dtype is None and is_datetime_or_timedelta_dtype(fill_dtype):
pytest.xfail("wrong missing value marker")

# create array of given dtype; casts "1" to correct dtype
fill_value = np.array([1], dtype=fill_dtype)[0]

Expand Down