24
24
25
25
import numpy as np
26
26
27
- from pandas ._libs import lib , missing as libmissing , tslib
27
+ from pandas ._libs import lib , tslib
28
28
from pandas ._libs .tslibs import (
29
29
NaT ,
30
30
OutOfBoundsDatetime ,
86
86
ABCSeries ,
87
87
)
88
88
from pandas .core .dtypes .inference import is_list_like
89
- from pandas .core .dtypes .missing import is_valid_na_for_dtype , isna , notna
89
+ from pandas .core .dtypes .missing import (
90
+ is_valid_na_for_dtype ,
91
+ isna ,
92
+ na_value_for_dtype ,
93
+ notna ,
94
+ )
90
95
91
96
if TYPE_CHECKING :
92
97
from pandas import Series
@@ -529,16 +534,26 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
529
534
dtype = np .dtype (object )
530
535
return dtype , fill_value
531
536
537
+ kinds = ["i" , "u" , "f" , "c" , "m" , "M" ]
538
+ if is_valid_na_for_dtype (fill_value , dtype ) and dtype .kind in kinds :
539
+ dtype = ensure_dtype_can_hold_na (dtype )
540
+ fv = na_value_for_dtype (dtype )
541
+ return dtype , fv
542
+
543
+ elif isna (fill_value ):
544
+ dtype = np .dtype (object )
545
+ if fill_value is None :
546
+ # but we retain e.g. pd.NA
547
+ fill_value = np .nan
548
+ return dtype , fill_value
549
+
532
550
# returns tuple of (dtype, fill_value)
533
551
if issubclass (dtype .type , np .datetime64 ):
534
552
if isinstance (fill_value , datetime ) and fill_value .tzinfo is not None :
535
553
# Trying to insert tzaware into tznaive, have to cast to object
536
554
dtype = np .dtype (np .object_ )
537
- elif is_integer (fill_value ) or ( is_float (fill_value ) and not isna ( fill_value ) ):
555
+ elif is_integer (fill_value ) or is_float (fill_value ):
538
556
dtype = np .dtype (np .object_ )
539
- elif is_valid_na_for_dtype (fill_value , dtype ):
540
- # e.g. pd.NA, which is not accepted by Timestamp constructor
541
- fill_value = np .datetime64 ("NaT" , "ns" )
542
557
else :
543
558
try :
544
559
fill_value = Timestamp (fill_value ).to_datetime64 ()
@@ -547,14 +562,11 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
547
562
elif issubclass (dtype .type , np .timedelta64 ):
548
563
if (
549
564
is_integer (fill_value )
550
- or ( is_float (fill_value ) and not np . isnan ( fill_value ) )
565
+ or is_float (fill_value )
551
566
or isinstance (fill_value , str )
552
567
):
553
568
# TODO: What about str that can be a timedelta?
554
569
dtype = np .dtype (np .object_ )
555
- elif is_valid_na_for_dtype (fill_value , dtype ):
556
- # e.g pd.NA, which is not accepted by the Timedelta constructor
557
- fill_value = np .timedelta64 ("NaT" , "ns" )
558
570
else :
559
571
try :
560
572
fv = Timedelta (fill_value )
@@ -615,17 +627,6 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
615
627
# e.g. mst is np.complex128 and dtype is np.complex64
616
628
dtype = mst
617
629
618
- elif fill_value is None or fill_value is libmissing .NA :
619
- # Note: we already excluded dt64/td64 dtypes above
620
- if is_float_dtype (dtype ) or is_complex_dtype (dtype ):
621
- fill_value = np .nan
622
- elif is_integer_dtype (dtype ):
623
- dtype = np .dtype (np .float64 )
624
- fill_value = np .nan
625
- else :
626
- dtype = np .dtype (np .object_ )
627
- if fill_value is not libmissing .NA :
628
- fill_value = np .nan
629
630
else :
630
631
dtype = np .dtype (np .object_ )
631
632
0 commit comments