|
36 | 36 | is_object_dtype)
|
37 | 37 | from pandas.core.dtypes.generic import ABCSeries, ABCDataFrame, ABCIndexClass
|
38 | 38 | from pandas.core.dtypes.dtypes import DatetimeTZDtype
|
| 39 | +from pandas.core.dtypes.missing import isna |
39 | 40 |
|
40 | 41 | import pandas.core.common as com
|
41 | 42 | from pandas.core.algorithms import checked_add_with_arr
|
@@ -370,6 +371,12 @@ def _add_timedeltalike_scalar(self, other):
|
370 | 371 | Add a delta of a timedeltalike
|
371 | 372 | return the i8 result view
|
372 | 373 | """
|
| 374 | + if isna(other): |
| 375 | + # i.e np.timedelta64("NaT"), not recognized by delta_to_nanoseconds |
| 376 | + new_values = np.empty(len(self), dtype='i8') |
| 377 | + new_values[:] = iNaT |
| 378 | + return new_values |
| 379 | + |
373 | 380 | inc = delta_to_nanoseconds(other)
|
374 | 381 | new_values = checked_add_with_arr(self.asi8, inc,
|
375 | 382 | arr_mask=self._isnan).view('i8')
|
@@ -442,7 +449,7 @@ def _sub_period_array(self, other):
|
442 | 449 | Array of DateOffset objects; nulls represented by NaT
|
443 | 450 | """
|
444 | 451 | if not is_period_dtype(self):
|
445 |
| - raise TypeError("cannot subtract {dtype}-dtype to {cls}" |
| 452 | + raise TypeError("cannot subtract {dtype}-dtype from {cls}" |
446 | 453 | .format(dtype=other.dtype,
|
447 | 454 | cls=type(self).__name__))
|
448 | 455 |
|
@@ -741,6 +748,11 @@ def __rsub__(self, other):
|
741 | 748 | raise TypeError("cannot subtract {cls} from {typ}"
|
742 | 749 | .format(cls=type(self).__name__,
|
743 | 750 | typ=type(other).__name__))
|
| 751 | + elif is_period_dtype(self) and is_timedelta64_dtype(other): |
| 752 | + # TODO: Can we simplify/generalize these cases at all? |
| 753 | + raise TypeError("cannot subtract {cls} from {dtype}" |
| 754 | + .format(cls=type(self).__name__, |
| 755 | + dtype=other.dtype)) |
744 | 756 | return -(self - other)
|
745 | 757 | cls.__rsub__ = __rsub__
|
746 | 758 |
|
|
0 commit comments