Skip to content

Commit acde02b

Browse files
jbrockmendeljreback
authored andcommitted
TST: fix 24 xfails in maybe_promote (#28833)
1 parent 29e56df commit acde02b

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

pandas/core/dtypes/cast.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,24 @@ def maybe_promote(dtype, fill_value=np.nan):
367367
except (TypeError, ValueError):
368368
dtype = np.dtype(np.object_)
369369
elif issubclass(dtype.type, np.timedelta64):
370-
try:
371-
fv = tslibs.Timedelta(fill_value)
372-
except ValueError:
370+
if (
371+
is_integer(fill_value)
372+
or (is_float(fill_value) and not np.isnan(fill_value))
373+
or isinstance(fill_value, str)
374+
):
375+
# TODO: What about str that can be a timedelta?
373376
dtype = np.dtype(np.object_)
374377
else:
375-
if fv is NaT:
376-
# NaT has no `to_timedelta64` method
377-
fill_value = np.timedelta64("NaT", "ns")
378+
try:
379+
fv = tslibs.Timedelta(fill_value)
380+
except ValueError:
381+
dtype = np.dtype(np.object_)
378382
else:
379-
fill_value = fv.to_timedelta64()
383+
if fv is NaT:
384+
# NaT has no `to_timedelta64` method
385+
fill_value = np.timedelta64("NaT", "ns")
386+
else:
387+
fill_value = fv.to_timedelta64()
380388
elif is_datetime64tz_dtype(dtype):
381389
if isna(fill_value):
382390
fill_value = NaT

pandas/core/internals/concat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ def get_empty_dtype_and_na(join_units):
338338
if not upcast_classes:
339339
upcast_classes = null_upcast_classes
340340

341+
# TODO: de-duplicate with maybe_promote?
341342
# create the result
342343
if "object" in upcast_classes:
343344
return np.dtype(np.object_), np.nan
@@ -356,7 +357,7 @@ def get_empty_dtype_and_na(join_units):
356357
elif "datetime" in upcast_classes:
357358
return np.dtype("M8[ns]"), tslibs.iNaT
358359
elif "timedelta" in upcast_classes:
359-
return np.dtype("m8[ns]"), tslibs.iNaT
360+
return np.dtype("m8[ns]"), np.timedelta64("NaT", "ns")
360361
else: # pragma
361362
try:
362363
g = np.find_common_type(upcast_classes, [])

pandas/tests/dtypes/cast/test_promote.py

-2
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,6 @@ def test_maybe_promote_timedelta64_with_any(
821821
else:
822822
if boxed and box_dtype is None:
823823
pytest.xfail("does not upcast to object")
824-
if not boxed:
825-
pytest.xfail("does not upcast to object or raises")
826824

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

0 commit comments

Comments
 (0)