File tree Expand file tree Collapse file tree 3 files changed +17
-10
lines changed Expand file tree Collapse file tree 3 files changed +17
-10
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 , [])
Original file line number Diff line number Diff 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 ]
You can’t perform that action at this time.
0 commit comments