File tree 1 file changed +13
-0
lines changed 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change 23
23
from pandas ._libs .missing import (
24
24
NA ,
25
25
NAType ,
26
+ checknull ,
26
27
)
27
28
from pandas ._libs .tslibs import (
28
29
NaT ,
@@ -556,6 +557,13 @@ def ensure_dtype_can_hold_na(dtype: DtypeObj) -> DtypeObj:
556
557
return dtype
557
558
558
559
560
+ _canonical_nans = {
561
+ np .datetime64 : np .datetime64 ("NaT" , "ns" ),
562
+ np .timedelta64 : np .timedelta64 ("NaT" , "ns" ),
563
+ type (np .nan ): np .nan ,
564
+ }
565
+
566
+
559
567
def maybe_promote (dtype : np .dtype , fill_value = np .nan ):
560
568
"""
561
569
Find the minimal dtype that can hold both the given dtype and fill_value.
@@ -577,6 +585,11 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
577
585
ValueError
578
586
If fill_value is a non-scalar and dtype is not object.
579
587
"""
588
+ if checknull (fill_value ):
589
+ # https://github.com/pandas-dev/pandas/pull/39692#issuecomment-1441051740
590
+ # avoid cache misses with NaN/NaT values that are not singletons
591
+ fill_value = _canonical_nans .get (type (fill_value ), fill_value )
592
+
580
593
# for performance, we are using a cached version of the actual implementation
581
594
# of the function in _maybe_promote. However, this doesn't always work (in case
582
595
# of non-hashable arguments), so we fallback to the actual implementation if needed
You can’t perform that action at this time.
0 commit comments