diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 7e3e9432be6ee..4435b2518e90b 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -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) @@ -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_ diff --git a/pandas/tests/dtypes/cast/test_promote.py b/pandas/tests/dtypes/cast/test_promote.py index a11fa78b7a3ab..8328f65518e02 100644 --- a/pandas/tests/dtypes/cast/test_promote.py +++ b/pandas/tests/dtypes/cast/test_promote.py @@ -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 @@ -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] @@ -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]