Skip to content

Commit d635620

Browse files
committed
TST: More maybe_promote xfails
1 parent 4e5c9d4 commit d635620

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

pandas/core/dtypes/cast.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,21 @@ 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+
351356
# returns tuple of (dtype, fill_value)
352357
if issubclass(dtype.type, np.datetime64):
353-
try:
354-
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
355-
except (TypeError, ValueError):
358+
if isinstance(fill_value, datetime) and fill_value.tzinfo is not None:
359+
# Trying to insert tzaware into tznaive, have to cast to object
356360
dtype = np.dtype(np.object_)
361+
else:
362+
try:
363+
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
364+
except (TypeError, ValueError):
365+
dtype = np.dtype(np.object_)
357366
elif issubclass(dtype.type, np.timedelta64):
358367
try:
359368
fv = tslibs.Timedelta(fill_value)

pandas/tests/dtypes/cast/test_promote.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,6 @@ def test_maybe_promote_any_numpy_dtype_with_datetimetz(
484484
fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture)
485485
boxed, box_dtype = box # read from parametrized fixture
486486

487-
if dtype.kind == "M" and not boxed:
488-
pytest.xfail("Comes back as M8 instead of object")
489-
490487
fill_value = pd.Series([fill_value], dtype=fill_dtype)[0]
491488

492489
# filling any numpy dtype with datetimetz casts to object
@@ -572,11 +569,6 @@ def test_maybe_promote_string_with_any(string_dtype, any_numpy_dtype_reduced, bo
572569
fill_dtype = np.dtype(any_numpy_dtype_reduced)
573570
boxed, box_dtype = box # read from parametrized fixture
574571

575-
if boxed and box_dtype is None and fill_dtype.kind == "m":
576-
pytest.xfail("wrong missing value marker")
577-
if boxed and box_dtype is None and fill_dtype.kind == "M":
578-
pytest.xfail("wrong missing value marker")
579-
580572
# create array of given dtype; casts "1" to correct dtype
581573
fill_value = np.array([1], dtype=fill_dtype)[0]
582574

@@ -639,9 +631,6 @@ def test_maybe_promote_object_with_any(object_dtype, any_numpy_dtype_reduced, bo
639631
fill_dtype = np.dtype(any_numpy_dtype_reduced)
640632
boxed, box_dtype = box # read from parametrized fixture
641633

642-
if boxed and box_dtype is None and is_datetime_or_timedelta_dtype(fill_dtype):
643-
pytest.xfail("wrong missing value marker")
644-
645634
# create array of given dtype; casts "1" to correct dtype
646635
fill_value = np.array([1], dtype=fill_dtype)[0]
647636

0 commit comments

Comments
 (0)