Skip to content

Commit

Permalink
TST: fix 24 xfails in maybe_promote (pandas-dev#28833)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent 3a4733a commit 8ab9e15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
22 changes: 15 additions & 7 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,24 @@ def maybe_promote(dtype, fill_value=np.nan):
except (TypeError, ValueError):
dtype = np.dtype(np.object_)
elif issubclass(dtype.type, np.timedelta64):
try:
fv = tslibs.Timedelta(fill_value)
except ValueError:
if (
is_integer(fill_value)
or (is_float(fill_value) and not np.isnan(fill_value))
or isinstance(fill_value, str)
):
# TODO: What about str that can be a timedelta?
dtype = np.dtype(np.object_)
else:
if fv is NaT:
# NaT has no `to_timedelta64` 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
3 changes: 2 additions & 1 deletion pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def get_empty_dtype_and_na(join_units):
if not upcast_classes:
upcast_classes = null_upcast_classes

# TODO: de-duplicate with maybe_promote?
# create the result
if "object" in upcast_classes:
return np.dtype(np.object_), np.nan
Expand All @@ -356,7 +357,7 @@ def get_empty_dtype_and_na(join_units):
elif "datetime" in upcast_classes:
return np.dtype("M8[ns]"), tslibs.iNaT
elif "timedelta" in upcast_classes:
return np.dtype("m8[ns]"), tslibs.iNaT
return np.dtype("m8[ns]"), np.timedelta64("NaT", "ns")
else: # pragma
try:
g = np.find_common_type(upcast_classes, [])
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,6 @@ def test_maybe_promote_timedelta64_with_any(
else:
if boxed and box_dtype is None:
pytest.xfail("does not upcast to object")
if not boxed:
pytest.xfail("does not upcast to object or raises")

# create array of given dtype; casts "1" to correct dtype
fill_value = np.array([1], dtype=fill_dtype)[0]
Expand Down

0 comments on commit 8ab9e15

Please sign in to comment.