Skip to content

TST: Fix broken test cases where Timedelta/Timestamp raise #28729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,21 @@ def maybe_promote(dtype, fill_value=np.nan):

# returns tuple of (dtype, fill_value)
if issubclass(dtype.type, np.datetime64):
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
try:
fill_value = tslibs.Timestamp(fill_value).to_datetime64()
except (TypeError, ValueError):
dtype = np.dtype(np.object_)
elif issubclass(dtype.type, np.timedelta64):
fv = tslibs.Timedelta(fill_value)
if fv is NaT:
# NaT has no `to_timedelta6` method
fill_value = np.timedelta64("NaT", "ns")
try:
fv = tslibs.Timedelta(fill_value)
except ValueError:
dtype = np.dtype(np.object_)
else:
fill_value = fv.to_timedelta64()
if fv is NaT:
# NaT has no `to_timedelta64` method
fill_value = np.timedelta64("NaT", "ns")
else:
fill_value = fv.to_timedelta64()
elif is_datetime64tz_dtype(dtype):
if isna(fill_value):
fill_value = NaT
Expand Down
18 changes: 1 addition & 17 deletions pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,6 @@ def test_maybe_promote_any_with_bool(any_numpy_dtype_reduced, box):
pytest.xfail("falsely upcasts to object")
if boxed and dtype not in (str, object) and box_dtype is None:
pytest.xfail("falsely upcasts to object")
if not boxed and dtype.kind == "M":
pytest.xfail("raises error")
if not boxed and dtype.kind == "m":
pytest.xfail("raises error")

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

# special case for box_dtype
box_dtype = np.dtype(datetime64_dtype) if box_dtype == "dt_dtype" else box_dtype
Expand Down Expand Up @@ -490,9 +484,7 @@ 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("raises error")
elif dtype.kind == "M" and not boxed:
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]
Expand Down Expand Up @@ -549,8 +541,6 @@ def test_maybe_promote_any_with_timedelta64(
else:
if boxed and box_dtype is None and is_timedelta64_dtype(type(fill_value)):
pytest.xfail("does not upcast correctly")
if not boxed and is_datetime64_dtype(dtype):
pytest.xfail("raises error")

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

if is_datetime_or_timedelta_dtype(dtype) and box_dtype != object:
pytest.xfail("does not upcast or raises")

# create array of given dtype
fill_value = "abc"

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

if not boxed and is_datetime_or_timedelta_dtype(dtype):
pytest.xfail("raises error")

# create array of object dtype from a scalar value (i.e. passing
# dtypes.common.is_scalar), which can however not be cast to int/float etc.
fill_value = pd.DateOffset(1)
Expand Down