@@ -475,7 +475,8 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray) -> np.ndarray:
475
475
# upcast (possibly), otherwise we DON't want to upcast (e.g. if we
476
476
# have values, say integers, in the success portion then it's ok to not
477
477
# upcast)
478
- new_dtype , _ = maybe_promote (result .dtype , np .nan )
478
+ new_dtype = ensure_dtype_can_hold_na (result .dtype )
479
+
479
480
if new_dtype != result .dtype :
480
481
result = result .astype (new_dtype , copy = True )
481
482
@@ -484,7 +485,21 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray) -> np.ndarray:
484
485
return result
485
486
486
487
487
- def maybe_promote (dtype , fill_value = np .nan ):
488
+ def ensure_dtype_can_hold_na (dtype : DtypeObj ) -> DtypeObj :
489
+ """
490
+ If we have a dtype that cannot hold NA values, find the best match that can.
491
+ """
492
+ if isinstance (dtype , ExtensionDtype ):
493
+ # TODO: ExtensionDtype.can_hold_na?
494
+ return dtype
495
+ elif dtype .kind == "b" :
496
+ return np .dtype (object )
497
+ elif dtype .kind in ["i" , "u" ]:
498
+ return np .dtype (np .float64 )
499
+ return dtype
500
+
501
+
502
+ def maybe_promote (dtype : DtypeObj , fill_value = np .nan ):
488
503
"""
489
504
Find the minimal dtype that can hold both the given dtype and fill_value.
490
505
@@ -565,7 +580,7 @@ def maybe_promote(dtype, fill_value=np.nan):
565
580
fill_value = np .timedelta64 ("NaT" , "ns" )
566
581
else :
567
582
fill_value = fv .to_timedelta64 ()
568
- elif is_datetime64tz_dtype (dtype ):
583
+ elif isinstance (dtype , DatetimeTZDtype ):
569
584
if isna (fill_value ):
570
585
fill_value = NaT
571
586
elif not isinstance (fill_value , datetime ):
0 commit comments