@@ -1300,7 +1300,7 @@ def __sub__(self, other):
13001300 return result
13011301
13021302 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 ):
13041304 # ndarray[datetime64] cannot be subtracted from self, so
13051305 # we need to wrap in DatetimeArray/Index and flip the operation
13061306 if not isinstance (other , DatetimeLikeArrayMixin ):
@@ -1310,9 +1310,9 @@ def __rsub__(self, other):
13101310 other = DatetimeArray (other )
13111311 return other - self
13121312 elif (
1313- is_datetime64_any_dtype (self )
1313+ is_datetime64_any_dtype (self . dtype )
13141314 and hasattr (other , "dtype" )
1315- and not is_datetime64_any_dtype (other )
1315+ and not is_datetime64_any_dtype (other . dtype )
13161316 ):
13171317 # GH#19959 datetime - datetime is well-defined as timedelta,
13181318 # but any other type - datetime is not well-defined.
@@ -1321,13 +1321,21 @@ def __rsub__(self, other):
13211321 cls = type (self ).__name__ , typ = type (other ).__name__
13221322 )
13231323 )
1324- elif is_period_dtype (self ) and is_timedelta64_dtype (other ):
1324+ elif is_period_dtype (self . dtype ) and is_timedelta64_dtype (other ):
13251325 # TODO: Can we simplify/generalize these cases at all?
13261326 raise TypeError (
13271327 "cannot subtract {cls} from {dtype}" .format (
13281328 cls = type (self ).__name__ , dtype = other .dtype
13291329 )
13301330 )
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+
13311339 return - (self - other )
13321340
13331341 # FIXME: DTA/TDA/PA inplace methods should actually be inplace, GH#24115
0 commit comments