Skip to content

Commit 412a412

Browse files
jbrockmendelproost
authored andcommitted
TST: Fix maybe_promote xfails (pandas-dev#28754)
1 parent b78c9f6 commit 412a412

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

pandas/core/dtypes/cast.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,22 @@ def maybe_promote(dtype, fill_value=np.nan):
348348
dtype = np.dtype(np.object_)
349349
fill_value = np.nan
350350

351+
if dtype == np.object_ or dtype.kind in ["U", "S"]:
352+
# We treat string-like dtypes as object, and _always_ fill
353+
# with np.nan
354+
fill_value = np.nan
355+
dtype = np.dtype(np.object_)
356+
351357
# returns tuple of (dtype, fill_value)
352358
if issubclass(dtype.type, np.datetime64):
353-
try:
354-
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
355-
except (TypeError, ValueError):
359+
if isinstance(fill_value, datetime) and fill_value.tzinfo is not None:
360+
# Trying to insert tzaware into tznaive, have to cast to object
356361
dtype = np.dtype(np.object_)
362+
else:
363+
try:
364+
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
365+
except (TypeError, ValueError):
366+
dtype = np.dtype(np.object_)
357367
elif issubclass(dtype.type, np.timedelta64):
358368
try:
359369
fv = tslibs.Timedelta(fill_value)
@@ -417,8 +427,6 @@ def maybe_promote(dtype, fill_value=np.nan):
417427
# in case we have a string that looked like a number
418428
if is_extension_array_dtype(dtype):
419429
pass
420-
elif is_datetime64tz_dtype(dtype):
421-
pass
422430
elif issubclass(np.dtype(dtype).type, (bytes, str)):
423431
dtype = np.object_
424432

pandas/tests/dtypes/cast/test_promote.py

-11
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,6 @@ def test_maybe_promote_any_numpy_dtype_with_datetimetz(
723723
fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture)
724724
boxed, box_dtype = box # read from parametrized fixture
725725

726-
if dtype.kind == "M" and not boxed:
727-
pytest.xfail("Comes back as M8 instead of object")
728-
729726
fill_value = pd.Series([fill_value], dtype=fill_dtype)[0]
730727

731728
# 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
847844
fill_dtype = np.dtype(any_numpy_dtype_reduced)
848845
boxed, box_dtype = box # read from parametrized fixture
849846

850-
if boxed and box_dtype is None and fill_dtype.kind == "m":
851-
pytest.xfail("wrong missing value marker")
852-
if boxed and box_dtype is None and fill_dtype.kind == "M":
853-
pytest.xfail("wrong missing value marker")
854-
855847
# create array of given dtype; casts "1" to correct dtype
856848
fill_value = np.array([1], dtype=fill_dtype)[0]
857849

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

917-
if boxed and box_dtype is None and is_datetime_or_timedelta_dtype(fill_dtype):
918-
pytest.xfail("wrong missing value marker")
919-
920909
# create array of given dtype; casts "1" to correct dtype
921910
fill_value = np.array([1], dtype=fill_dtype)[0]
922911

0 commit comments

Comments
 (0)