@@ -1300,7 +1300,7 @@ def __sub__(self, other):
1300
1300
return result
1301
1301
1302
1302
def __rsub__ (self , other ):
1303
- if is_datetime64_any_dtype (other ) and is_timedelta64_dtype (self ):
1303
+ if is_datetime64_any_dtype (other ) and is_timedelta64_dtype (self . dtype ):
1304
1304
# ndarray[datetime64] cannot be subtracted from self, so
1305
1305
# we need to wrap in DatetimeArray/Index and flip the operation
1306
1306
if not isinstance (other , DatetimeLikeArrayMixin ):
@@ -1310,9 +1310,9 @@ def __rsub__(self, other):
1310
1310
other = DatetimeArray (other )
1311
1311
return other - self
1312
1312
elif (
1313
- is_datetime64_any_dtype (self )
1313
+ is_datetime64_any_dtype (self . dtype )
1314
1314
and hasattr (other , "dtype" )
1315
- and not is_datetime64_any_dtype (other )
1315
+ and not is_datetime64_any_dtype (other . dtype )
1316
1316
):
1317
1317
# GH#19959 datetime - datetime is well-defined as timedelta,
1318
1318
# but any other type - datetime is not well-defined.
@@ -1321,13 +1321,21 @@ def __rsub__(self, other):
1321
1321
cls = type (self ).__name__ , typ = type (other ).__name__
1322
1322
)
1323
1323
)
1324
- elif is_period_dtype (self ) and is_timedelta64_dtype (other ):
1324
+ elif is_period_dtype (self . dtype ) and is_timedelta64_dtype (other ):
1325
1325
# TODO: Can we simplify/generalize these cases at all?
1326
1326
raise TypeError (
1327
1327
"cannot subtract {cls} from {dtype}" .format (
1328
1328
cls = type (self ).__name__ , dtype = other .dtype
1329
1329
)
1330
1330
)
1331
+ elif is_timedelta64_dtype (self .dtype ):
1332
+ if lib .is_integer (other ) or is_integer_dtype (other ):
1333
+ # need to subtract before negating, since that flips freq
1334
+ # -self flips self.freq, messing up results
1335
+ return - (self - other )
1336
+
1337
+ return (- self ) + other
1338
+
1331
1339
return - (self - other )
1332
1340
1333
1341
# FIXME: DTA/TDA/PA inplace methods should actually be inplace, GH#24115
0 commit comments