9
9
10
10
from pandas ._libs import NaT , lib , tslib , tslibs
11
11
import pandas ._libs .internals as libinternals
12
- from pandas ._libs .tslibs import Timedelta , conversion , is_null_datetimelike
12
+ from pandas ._libs .tslibs import Timedelta , conversion
13
+ from pandas ._libs .tslibs .timezones import tz_compare
13
14
from pandas .util ._validators import validate_bool_kwarg
14
15
15
16
from pandas .core .dtypes .cast import (
60
61
ABCPandasArray ,
61
62
ABCSeries ,
62
63
)
63
- from pandas .core .dtypes .missing import _isna_compat , array_equivalent , isna , notna
64
+ from pandas .core .dtypes .missing import (
65
+ _isna_compat ,
66
+ array_equivalent ,
67
+ is_valid_nat_for_dtype ,
68
+ isna ,
69
+ notna ,
70
+ )
64
71
65
72
import pandas .core .algorithms as algos
66
73
from pandas .core .arrays import (
@@ -2248,14 +2255,17 @@ def _astype(self, dtype, **kwargs):
2248
2255
def _can_hold_element (self , element ):
2249
2256
tipo = maybe_infer_dtype_type (element )
2250
2257
if tipo is not None :
2251
- return tipo == _NS_DTYPE or tipo == np .int64
2258
+ return is_dtype_equal (tipo , self .dtype )
2259
+ elif element is NaT :
2260
+ return True
2252
2261
elif isinstance (element , datetime ):
2262
+ if self .is_datetimetz :
2263
+ return tz_compare (element .tzinfo , self .dtype .tz )
2253
2264
return element .tzinfo is None
2254
2265
elif is_integer (element ):
2255
2266
return element == tslibs .iNaT
2256
2267
2257
- # TODO: shouldnt we exclude timedelta64("NaT")? See GH#27297
2258
- return isna (element )
2268
+ return is_valid_nat_for_dtype (element , self .dtype )
2259
2269
2260
2270
def _coerce_values (self , values ):
2261
2271
return values .view ("i8" )
@@ -2275,8 +2285,10 @@ def _try_coerce_args(self, other):
2275
2285
-------
2276
2286
base-type other
2277
2287
"""
2278
- if is_null_datetimelike (other ):
2288
+ if is_valid_nat_for_dtype (other , self . dtype ):
2279
2289
other = tslibs .iNaT
2290
+ elif is_integer (other ) and other == tslibs .iNaT :
2291
+ pass
2280
2292
elif isinstance (other , (datetime , np .datetime64 , date )):
2281
2293
other = self ._box_func (other )
2282
2294
if getattr (other , "tz" ) is not None :
@@ -2359,6 +2371,8 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
2359
2371
is_datetimetz = True
2360
2372
is_extension = True
2361
2373
2374
+ _can_hold_element = DatetimeBlock ._can_hold_element
2375
+
2362
2376
@property
2363
2377
def _holder (self ):
2364
2378
return DatetimeArray
@@ -2465,8 +2479,10 @@ def _try_coerce_args(self, other):
2465
2479
# add the tz back
2466
2480
other = self ._holder (other , dtype = self .dtype )
2467
2481
2468
- elif is_null_datetimelike (other ):
2482
+ elif is_valid_nat_for_dtype (other , self . dtype ):
2469
2483
other = tslibs .iNaT
2484
+ elif is_integer (other ) and other == tslibs .iNaT :
2485
+ pass
2470
2486
elif isinstance (other , self ._holder ):
2471
2487
if other .tz != self .values .tz :
2472
2488
raise ValueError ("incompatible or non tz-aware value" )
@@ -2606,10 +2622,16 @@ def _box_func(self):
2606
2622
def _can_hold_element (self , element ):
2607
2623
tipo = maybe_infer_dtype_type (element )
2608
2624
if tipo is not None :
2625
+ # TODO: remove the np.int64 support once coerce_values and
2626
+ # _try_coerce_args both coerce to m8[ns] and not i8.
2609
2627
return issubclass (tipo .type , (np .timedelta64 , np .int64 ))
2610
2628
elif element is NaT :
2611
2629
return True
2612
- return is_integer (element ) or isinstance (element , (timedelta , np .timedelta64 ))
2630
+ elif isinstance (element , (timedelta , np .timedelta64 )):
2631
+ return True
2632
+ elif is_integer (element ):
2633
+ return element == tslibs .iNaT
2634
+ return is_valid_nat_for_dtype (element , self .dtype )
2613
2635
2614
2636
def fillna (self , value , ** kwargs ):
2615
2637
@@ -2645,8 +2667,10 @@ def _try_coerce_args(self, other):
2645
2667
base-type other
2646
2668
"""
2647
2669
2648
- if is_null_datetimelike (other ):
2670
+ if is_valid_nat_for_dtype (other , self . dtype ):
2649
2671
other = tslibs .iNaT
2672
+ elif is_integer (other ) and other == tslibs .iNaT :
2673
+ pass
2650
2674
elif isinstance (other , (timedelta , np .timedelta64 )):
2651
2675
other = Timedelta (other ).value
2652
2676
elif hasattr (other , "dtype" ) and is_timedelta64_dtype (other ):
0 commit comments