Skip to content

Commit bc038e7

Browse files
jbrockmendelproost
authored andcommitted
TST: Fix broken test cases where Timedelta/Timestamp raise (pandas-dev#28729)
1 parent bfbc0cc commit bc038e7

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

pandas/core/dtypes/cast.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,21 @@ def maybe_promote(dtype, fill_value=np.nan):
350350

351351
# returns tuple of (dtype, fill_value)
352352
if issubclass(dtype.type, np.datetime64):
353-
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
353+
try:
354+
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
355+
except (TypeError, ValueError):
356+
dtype = np.dtype(np.object_)
354357
elif issubclass(dtype.type, np.timedelta64):
355-
fv = tslibs.Timedelta(fill_value)
356-
if fv is NaT:
357-
# NaT has no `to_timedelta6` method
358-
fill_value = np.timedelta64("NaT", "ns")
358+
try:
359+
fv = tslibs.Timedelta(fill_value)
360+
except ValueError:
361+
dtype = np.dtype(np.object_)
359362
else:
360-
fill_value = fv.to_timedelta64()
363+
if fv is NaT:
364+
# NaT has no `to_timedelta64` method
365+
fill_value = np.timedelta64("NaT", "ns")
366+
else:
367+
fill_value = fv.to_timedelta64()
361368
elif is_datetime64tz_dtype(dtype):
362369
if isna(fill_value):
363370
fill_value = NaT

pandas/tests/dtypes/cast/test_promote.py

+1-17
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,6 @@ def test_maybe_promote_any_with_bool(any_numpy_dtype_reduced, box):
272272
pytest.xfail("falsely upcasts to object")
273273
if boxed and dtype not in (str, object) and box_dtype is None:
274274
pytest.xfail("falsely upcasts to object")
275-
if not boxed and dtype.kind == "M":
276-
pytest.xfail("raises error")
277-
if not boxed and dtype.kind == "m":
278-
pytest.xfail("raises error")
279275

280276
# filling anything but bool with bool casts to object
281277
expected_dtype = np.dtype(object) if dtype != bool else dtype
@@ -348,8 +344,6 @@ def test_maybe_promote_any_with_datetime64(
348344
or (box_dtype is None and is_datetime64_dtype(type(fill_value)))
349345
):
350346
pytest.xfail("mix of lack of upcasting, resp. wrong missing value")
351-
if not boxed and is_timedelta64_dtype(dtype):
352-
pytest.xfail("raises error")
353347

354348
# special case for box_dtype
355349
box_dtype = np.dtype(datetime64_dtype) if box_dtype == "dt_dtype" else box_dtype
@@ -490,9 +484,7 @@ def test_maybe_promote_any_numpy_dtype_with_datetimetz(
490484
fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture)
491485
boxed, box_dtype = box # read from parametrized fixture
492486

493-
if dtype.kind == "m" and not boxed:
494-
pytest.xfail("raises error")
495-
elif dtype.kind == "M" and not boxed:
487+
if dtype.kind == "M" and not boxed:
496488
pytest.xfail("Comes back as M8 instead of object")
497489

498490
fill_value = pd.Series([fill_value], dtype=fill_dtype)[0]
@@ -549,8 +541,6 @@ def test_maybe_promote_any_with_timedelta64(
549541
else:
550542
if boxed and box_dtype is None and is_timedelta64_dtype(type(fill_value)):
551543
pytest.xfail("does not upcast correctly")
552-
if not boxed and is_datetime64_dtype(dtype):
553-
pytest.xfail("raises error")
554544

555545
# special case for box_dtype
556546
box_dtype = np.dtype(timedelta64_dtype) if box_dtype == "td_dtype" else box_dtype
@@ -622,9 +612,6 @@ def test_maybe_promote_any_with_string(any_numpy_dtype_reduced, string_dtype, bo
622612
fill_dtype = np.dtype(string_dtype)
623613
boxed, box_dtype = box # read from parametrized fixture
624614

625-
if is_datetime_or_timedelta_dtype(dtype) and box_dtype != object:
626-
pytest.xfail("does not upcast or raises")
627-
628615
# create array of given dtype
629616
fill_value = "abc"
630617

@@ -678,9 +665,6 @@ def test_maybe_promote_any_with_object(any_numpy_dtype_reduced, object_dtype, bo
678665
dtype = np.dtype(any_numpy_dtype_reduced)
679666
boxed, box_dtype = box # read from parametrized fixture
680667

681-
if not boxed and is_datetime_or_timedelta_dtype(dtype):
682-
pytest.xfail("raises error")
683-
684668
# create array of object dtype from a scalar value (i.e. passing
685669
# dtypes.common.is_scalar), which can however not be cast to int/float etc.
686670
fill_value = pd.DateOffset(1)

0 commit comments

Comments
 (0)