84
84
from pandas .core .dtypes .common import (
85
85
is_all_strings ,
86
86
is_datetime64_any_dtype ,
87
- is_datetime64_dtype ,
88
87
is_datetime_or_timedelta_dtype ,
89
88
is_dtype_equal ,
90
89
is_float_dtype ,
91
90
is_integer_dtype ,
92
91
is_list_like ,
93
92
is_object_dtype ,
94
93
is_string_dtype ,
95
- is_timedelta64_dtype ,
96
94
pandas_dtype ,
97
95
)
98
96
from pandas .core .dtypes .dtypes import (
@@ -993,7 +991,7 @@ def _get_arithmetic_result_freq(self, other) -> BaseOffset | None:
993
991
994
992
@final
995
993
def _add_datetimelike_scalar (self , other ) -> DatetimeArray :
996
- if not is_timedelta64_dtype (self .dtype ):
994
+ if not lib . is_np_dtype (self .dtype , "m" ):
997
995
raise TypeError (
998
996
f"cannot add { type (self ).__name__ } and { type (other ).__name__ } "
999
997
)
@@ -1029,7 +1027,7 @@ def _add_datetimelike_scalar(self, other) -> DatetimeArray:
1029
1027
1030
1028
@final
1031
1029
def _add_datetime_arraylike (self , other : DatetimeArray ) -> DatetimeArray :
1032
- if not is_timedelta64_dtype (self .dtype ):
1030
+ if not lib . is_np_dtype (self .dtype , "m" ):
1033
1031
raise TypeError (
1034
1032
f"cannot add { type (self ).__name__ } and { type (other ).__name__ } "
1035
1033
)
@@ -1093,7 +1091,7 @@ def _sub_datetimelike(self, other: Timestamp | DatetimeArray) -> TimedeltaArray:
1093
1091
1094
1092
@final
1095
1093
def _add_period (self , other : Period ) -> PeriodArray :
1096
- if not is_timedelta64_dtype (self .dtype ):
1094
+ if not lib . is_np_dtype (self .dtype , "m" ):
1097
1095
raise TypeError (f"cannot add Period to a { type (self ).__name__ } " )
1098
1096
1099
1097
# We will wrap in a PeriodArray and defer to the reversed operation
@@ -1294,7 +1292,7 @@ def __add__(self, other):
1294
1292
result = self ._add_offset (other )
1295
1293
elif isinstance (other , (datetime , np .datetime64 )):
1296
1294
result = self ._add_datetimelike_scalar (other )
1297
- elif isinstance (other , Period ) and is_timedelta64_dtype (self .dtype ):
1295
+ elif isinstance (other , Period ) and lib . is_np_dtype (self .dtype , "m" ):
1298
1296
result = self ._add_period (other )
1299
1297
elif lib .is_integer (other ):
1300
1298
# This check must come after the check for np.timedelta64
@@ -1305,13 +1303,13 @@ def __add__(self, other):
1305
1303
result = obj ._addsub_int_array_or_scalar (other * obj .dtype ._n , operator .add )
1306
1304
1307
1305
# array-like others
1308
- elif is_timedelta64_dtype (other_dtype ):
1306
+ elif lib . is_np_dtype (other_dtype , "m" ):
1309
1307
# TimedeltaIndex, ndarray[timedelta64]
1310
1308
result = self ._add_timedelta_arraylike (other )
1311
1309
elif is_object_dtype (other_dtype ):
1312
1310
# e.g. Array/Index of DateOffset objects
1313
1311
result = self ._addsub_object_array (other , operator .add )
1314
- elif is_datetime64_dtype (other_dtype ) or isinstance (
1312
+ elif lib . is_np_dtype (other_dtype , "M" ) or isinstance (
1315
1313
other_dtype , DatetimeTZDtype
1316
1314
):
1317
1315
# DatetimeIndex, ndarray[datetime64]
@@ -1329,7 +1327,7 @@ def __add__(self, other):
1329
1327
# In remaining cases, this will end up raising TypeError.
1330
1328
return NotImplemented
1331
1329
1332
- if isinstance (result , np .ndarray ) and is_timedelta64_dtype (result .dtype ):
1330
+ if isinstance (result , np .ndarray ) and lib . is_np_dtype (result .dtype , "m" ):
1333
1331
from pandas .core .arrays import TimedeltaArray
1334
1332
1335
1333
return TimedeltaArray (result )
@@ -1366,13 +1364,13 @@ def __sub__(self, other):
1366
1364
result = self ._sub_periodlike (other )
1367
1365
1368
1366
# array-like others
1369
- elif is_timedelta64_dtype (other_dtype ):
1367
+ elif lib . is_np_dtype (other_dtype , "m" ):
1370
1368
# TimedeltaIndex, ndarray[timedelta64]
1371
1369
result = self ._add_timedelta_arraylike (- other )
1372
1370
elif is_object_dtype (other_dtype ):
1373
1371
# e.g. Array/Index of DateOffset objects
1374
1372
result = self ._addsub_object_array (other , operator .sub )
1375
- elif is_datetime64_dtype (other_dtype ) or isinstance (
1373
+ elif lib . is_np_dtype (other_dtype , "M" ) or isinstance (
1376
1374
other_dtype , DatetimeTZDtype
1377
1375
):
1378
1376
# DatetimeIndex, ndarray[datetime64]
@@ -1389,7 +1387,7 @@ def __sub__(self, other):
1389
1387
# Includes ExtensionArrays, float_dtype
1390
1388
return NotImplemented
1391
1389
1392
- if isinstance (result , np .ndarray ) and is_timedelta64_dtype (result .dtype ):
1390
+ if isinstance (result , np .ndarray ) and lib . is_np_dtype (result .dtype , "m" ):
1393
1391
from pandas .core .arrays import TimedeltaArray
1394
1392
1395
1393
return TimedeltaArray (result )
@@ -1398,7 +1396,7 @@ def __sub__(self, other):
1398
1396
def __rsub__ (self , other ):
1399
1397
other_dtype = getattr (other , "dtype" , None )
1400
1398
1401
- if is_datetime64_any_dtype (other_dtype ) and is_timedelta64_dtype (self .dtype ):
1399
+ if is_datetime64_any_dtype (other_dtype ) and lib . is_np_dtype (self .dtype , "m" ):
1402
1400
# ndarray[datetime64] cannot be subtracted from self, so
1403
1401
# we need to wrap in DatetimeArray/Index and flip the operation
1404
1402
if lib .is_scalar (other ):
@@ -1420,10 +1418,10 @@ def __rsub__(self, other):
1420
1418
raise TypeError (
1421
1419
f"cannot subtract { type (self ).__name__ } from { type (other ).__name__ } "
1422
1420
)
1423
- elif isinstance (self .dtype , PeriodDtype ) and is_timedelta64_dtype (other_dtype ):
1421
+ elif isinstance (self .dtype , PeriodDtype ) and lib . is_np_dtype (other_dtype , "m" ):
1424
1422
# TODO: Can we simplify/generalize these cases at all?
1425
1423
raise TypeError (f"cannot subtract { type (self ).__name__ } from { other .dtype } " )
1426
- elif is_timedelta64_dtype (self .dtype ):
1424
+ elif lib . is_np_dtype (self .dtype , "m" ):
1427
1425
self = cast ("TimedeltaArray" , self )
1428
1426
return (- self ) + other
1429
1427
0 commit comments