8
8
import numpy as np
9
9
10
10
from pandas ._libs import internals as libinternals , lib , tslib , tslibs
11
- from pandas ._libs .tslibs import Timedelta , conversion
11
+ from pandas ._libs .tslibs import Timedelta , conversion , is_null_datetimelike
12
12
import pandas .compat as compat
13
13
from pandas .compat import range , zip
14
14
from pandas .util ._validators import validate_bool_kwarg
31
31
ABCDataFrame , ABCDatetimeIndex , ABCExtensionArray , ABCIndexClass ,
32
32
ABCSeries )
33
33
from pandas .core .dtypes .missing import (
34
- _isna_compat , array_equivalent , is_null_datelike_scalar , isna , notna )
34
+ _isna_compat , array_equivalent , isna , notna )
35
35
36
36
import pandas .core .algorithms as algos
37
37
from pandas .core .arrays import (
@@ -2085,10 +2085,6 @@ def get_values(self, dtype=None):
2085
2085
return values
2086
2086
return self .values
2087
2087
2088
- @property
2089
- def asi8 (self ):
2090
- return self .values .view ('i8' )
2091
-
2092
2088
2093
2089
class DatetimeBlock (DatetimeLikeBlockMixin , Block ):
2094
2090
__slots__ = ()
@@ -2170,7 +2166,7 @@ def _try_coerce_args(self, values, other):
2170
2166
2171
2167
if isinstance (other , bool ):
2172
2168
raise TypeError
2173
- elif is_null_datelike_scalar (other ):
2169
+ elif is_null_datetimelike (other ):
2174
2170
other = tslibs .iNaT
2175
2171
elif isinstance (other , (datetime , np .datetime64 , date )):
2176
2172
other = self ._box_func (other )
@@ -2183,18 +2179,16 @@ def _try_coerce_args(self, values, other):
2183
2179
else :
2184
2180
# coercion issues
2185
2181
# let higher levels handle
2186
- raise TypeError
2182
+ raise TypeError ( other )
2187
2183
2188
2184
return values , other
2189
2185
2190
2186
def _try_coerce_result (self , result ):
2191
2187
""" reverse of try_coerce_args """
2192
2188
if isinstance (result , np .ndarray ):
2193
- if result .dtype .kind in ['i' , 'f' , 'O' ]:
2194
- try :
2195
- result = result .astype ('M8[ns]' )
2196
- except ValueError :
2197
- pass
2189
+ if result .dtype .kind in ['i' , 'f' ]:
2190
+ result = result .astype ('M8[ns]' )
2191
+
2198
2192
elif isinstance (result , (np .integer , np .float , np .datetime64 )):
2199
2193
result = self ._box_func (result )
2200
2194
return result
@@ -2378,8 +2372,7 @@ def _try_coerce_args(self, values, other):
2378
2372
# add the tz back
2379
2373
other = self ._holder (other , dtype = self .dtype )
2380
2374
2381
- elif (is_null_datelike_scalar (other ) or
2382
- (lib .is_scalar (other ) and isna (other ))):
2375
+ elif is_null_datetimelike (other ):
2383
2376
other = tslibs .iNaT
2384
2377
elif isinstance (other , self ._holder ):
2385
2378
if other .tz != self .values .tz :
@@ -2394,17 +2387,19 @@ def _try_coerce_args(self, values, other):
2394
2387
raise ValueError ("incompatible or non tz-aware value" )
2395
2388
other = other .value
2396
2389
else :
2397
- raise TypeError
2390
+ raise TypeError ( other )
2398
2391
2399
2392
return values , other
2400
2393
2401
2394
def _try_coerce_result (self , result ):
2402
2395
""" reverse of try_coerce_args """
2403
2396
if isinstance (result , np .ndarray ):
2404
- if result .dtype .kind in ['i' , 'f' , 'O' ]:
2397
+ if result .dtype .kind in ['i' , 'f' ]:
2405
2398
result = result .astype ('M8[ns]' )
2399
+
2406
2400
elif isinstance (result , (np .integer , np .float , np .datetime64 )):
2407
2401
result = self ._box_func (result )
2402
+
2408
2403
if isinstance (result , np .ndarray ):
2409
2404
# allow passing of > 1dim if its trivial
2410
2405
@@ -2545,32 +2540,30 @@ def _try_coerce_args(self, values, other):
2545
2540
2546
2541
if isinstance (other , bool ):
2547
2542
raise TypeError
2548
- elif is_null_datelike_scalar (other ):
2543
+ elif is_null_datetimelike (other ):
2549
2544
other = tslibs .iNaT
2550
- elif isinstance (other , Timedelta ):
2551
- other = other .value
2552
- elif isinstance (other , timedelta ):
2553
- other = Timedelta (other ).value
2554
- elif isinstance (other , np .timedelta64 ):
2545
+ elif isinstance (other , (timedelta , np .timedelta64 )):
2555
2546
other = Timedelta (other ).value
2556
2547
elif hasattr (other , 'dtype' ) and is_timedelta64_dtype (other ):
2557
2548
other = other .astype ('i8' , copy = False ).view ('i8' )
2558
2549
else :
2559
2550
# coercion issues
2560
2551
# let higher levels handle
2561
- raise TypeError
2552
+ raise TypeError ( other )
2562
2553
2563
2554
return values , other
2564
2555
2565
2556
def _try_coerce_result (self , result ):
2566
2557
""" reverse of try_coerce_args / try_operate """
2567
2558
if isinstance (result , np .ndarray ):
2568
2559
mask = isna (result )
2569
- if result .dtype .kind in ['i' , 'f' , 'O' ]:
2560
+ if result .dtype .kind in ['i' , 'f' ]:
2570
2561
result = result .astype ('m8[ns]' )
2571
2562
result [mask ] = tslibs .iNaT
2563
+
2572
2564
elif isinstance (result , (np .integer , np .float )):
2573
2565
result = self ._box_func (result )
2566
+
2574
2567
return result
2575
2568
2576
2569
def should_store (self , value ):
0 commit comments