@@ -363,7 +363,7 @@ class TimelikeOps:
363
363
364
364
def _round (self , freq , mode , ambiguous , nonexistent ):
365
365
# round the local times
366
- if is_datetime64tz_dtype (self ):
366
+ if is_datetime64tz_dtype (self . dtype ):
367
367
# operate on naive timestamps, then convert back to aware
368
368
naive = self .tz_localize (None )
369
369
result = naive ._round (freq , mode , ambiguous , nonexistent )
@@ -1032,7 +1032,7 @@ def fillna(self, value=None, method=None, limit=None):
1032
1032
values = values .copy ()
1033
1033
1034
1034
new_values = func (values , limit = limit , mask = mask )
1035
- if is_datetime64tz_dtype (self ):
1035
+ if is_datetime64tz_dtype (self . dtype ):
1036
1036
# we need to pass int64 values to the constructor to avoid
1037
1037
# re-localizing incorrectly
1038
1038
new_values = new_values .view ("i8" )
@@ -1379,6 +1379,7 @@ def _time_shift(self, periods, freq=None):
1379
1379
1380
1380
@unpack_zerodim_and_defer ("__add__" )
1381
1381
def __add__ (self , other ):
1382
+ other_dtype = getattr (other , "dtype" , None )
1382
1383
1383
1384
# scalar others
1384
1385
if other is NaT :
@@ -1398,16 +1399,16 @@ def __add__(self, other):
1398
1399
result = self ._time_shift (other )
1399
1400
1400
1401
# array-like others
1401
- elif is_timedelta64_dtype (other ):
1402
+ elif is_timedelta64_dtype (other_dtype ):
1402
1403
# TimedeltaIndex, ndarray[timedelta64]
1403
1404
result = self ._add_timedelta_arraylike (other )
1404
- elif is_object_dtype (other ):
1405
+ elif is_object_dtype (other_dtype ):
1405
1406
# e.g. Array/Index of DateOffset objects
1406
1407
result = self ._addsub_object_array (other , operator .add )
1407
- elif is_datetime64_dtype (other ) or is_datetime64tz_dtype (other ):
1408
+ elif is_datetime64_dtype (other_dtype ) or is_datetime64tz_dtype (other_dtype ):
1408
1409
# DatetimeIndex, ndarray[datetime64]
1409
1410
return self ._add_datetime_arraylike (other )
1410
- elif is_integer_dtype (other ):
1411
+ elif is_integer_dtype (other_dtype ):
1411
1412
if not is_period_dtype (self .dtype ):
1412
1413
raise integer_op_not_supported (self )
1413
1414
result = self ._addsub_int_array (other , operator .add )
@@ -1419,7 +1420,7 @@ def __add__(self, other):
1419
1420
# In remaining cases, this will end up raising TypeError.
1420
1421
return NotImplemented
1421
1422
1422
- if is_timedelta64_dtype (result ) and isinstance (result , np . ndarray ):
1423
+ if isinstance (result , np . ndarray ) and is_timedelta64_dtype (result . dtype ):
1423
1424
from pandas .core .arrays import TimedeltaArray
1424
1425
1425
1426
return TimedeltaArray (result )
@@ -1455,13 +1456,13 @@ def __sub__(self, other):
1455
1456
result = self ._sub_period (other )
1456
1457
1457
1458
# array-like others
1458
- elif is_timedelta64_dtype (other ):
1459
+ elif is_timedelta64_dtype (other_dtype ):
1459
1460
# TimedeltaIndex, ndarray[timedelta64]
1460
1461
result = self ._add_timedelta_arraylike (- other )
1461
- elif is_object_dtype (other ):
1462
+ elif is_object_dtype (other_dtype ):
1462
1463
# e.g. Array/Index of DateOffset objects
1463
1464
result = self ._addsub_object_array (other , operator .sub )
1464
- elif is_datetime64_dtype (other ) or is_datetime64tz_dtype (other ):
1465
+ elif is_datetime64_dtype (other_dtype ) or is_datetime64tz_dtype (other_dtype ):
1465
1466
# DatetimeIndex, ndarray[datetime64]
1466
1467
result = self ._sub_datetime_arraylike (other )
1467
1468
elif is_period_dtype (other_dtype ):
@@ -1475,14 +1476,16 @@ def __sub__(self, other):
1475
1476
# Includes ExtensionArrays, float_dtype
1476
1477
return NotImplemented
1477
1478
1478
- if is_timedelta64_dtype (result ) and isinstance (result , np . ndarray ):
1479
+ if isinstance (result , np . ndarray ) and is_timedelta64_dtype (result . dtype ):
1479
1480
from pandas .core .arrays import TimedeltaArray
1480
1481
1481
1482
return TimedeltaArray (result )
1482
1483
return result
1483
1484
1484
1485
def __rsub__ (self , other ):
1485
- if is_datetime64_any_dtype (other ) and is_timedelta64_dtype (self .dtype ):
1486
+ other_dtype = getattr (other , "dtype" , None )
1487
+
1488
+ if is_datetime64_any_dtype (other_dtype ) and is_timedelta64_dtype (self .dtype ):
1486
1489
# ndarray[datetime64] cannot be subtracted from self, so
1487
1490
# we need to wrap in DatetimeArray/Index and flip the operation
1488
1491
if lib .is_scalar (other ):
@@ -1504,7 +1507,7 @@ def __rsub__(self, other):
1504
1507
raise TypeError (
1505
1508
f"cannot subtract { type (self ).__name__ } from { type (other ).__name__ } "
1506
1509
)
1507
- elif is_period_dtype (self .dtype ) and is_timedelta64_dtype (other ):
1510
+ elif is_period_dtype (self .dtype ) and is_timedelta64_dtype (other_dtype ):
1508
1511
# TODO: Can we simplify/generalize these cases at all?
1509
1512
raise TypeError (f"cannot subtract { type (self ).__name__ } from { other .dtype } " )
1510
1513
elif is_timedelta64_dtype (self .dtype ):
0 commit comments