Move Unchanged arith methods to EA mixins (#21712) · pandas-dev/pandas@2b13605 (original) (raw)
6 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -167,3 +167,15 @@ def _add_delta_tdi(self, other): | ||
167 | 167 | mask = (self._isnan) | (other._isnan) |
168 | 168 | new_values[mask] = iNaT |
169 | 169 | return new_values.view('i8') |
170 | + | |
171 | +def _sub_nat(self): | |
172 | +"""Subtract pd.NaT from self""" | |
173 | +# GH#19124 Timedelta - datetime is not in general well-defined. | |
174 | +# We make an exception for pd.NaT, which in this case quacks | |
175 | +# like a timedelta. | |
176 | +# For datetime64 dtypes by convention we treat NaT as a datetime, so | |
177 | +# this subtraction returns a timedelta64 dtype. | |
178 | +# For period dtype, timedelta64 is a close-enough return dtype. | |
179 | +result = np.zeros(len(self), dtype=np.int64) | |
180 | +result.fill(iNaT) | |
181 | +return result.view('timedelta64[ns]') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | |
3 | +from pandas._libs.tslib import NaT | |
3 | 4 | from pandas._libs.tslibs.period import Period |
4 | 5 | |
5 | 6 | from pandas.util._decorators import cache_readonly |
@@ -26,3 +27,10 @@ def _ndarray_values(self): | ||
26 | 27 | @property |
27 | 28 | def asi8(self): |
28 | 29 | return self._ndarray_values.view('i8') |
30 | + | |
31 | +# ------------------------------------------------------------------ | |
32 | +# Arithmetic Methods | |
33 | + | |
34 | +def _sub_datelike(self, other): | |
35 | +assert other is not NaT | |
36 | +return NotImplemented |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | |
3 | -from pandas._libs.tslib import Timedelta | |
3 | +from pandas._libs.tslib import Timedelta, NaT | |
4 | 4 | |
5 | 5 | from pandas.core.dtypes.common import _TD_DTYPE |
6 | 6 | |
7 | +from pandas.tseries.offsets import Tick | |
8 | + | |
7 | 9 | from .datetimelike import DatetimeLikeArrayMixin |
8 | 10 | |
9 | 11 | |
@@ -15,3 +17,17 @@ def _box_func(self): | ||
15 | 17 | @property |
16 | 18 | def dtype(self): |
17 | 19 | return _TD_DTYPE |
20 | + | |
21 | +# ---------------------------------------------------------------- | |
22 | +# Arithmetic Methods | |
23 | + | |
24 | +def _add_offset(self, other): | |
25 | +assert not isinstance(other, Tick) | |
26 | +raise TypeError("cannot add the type {typ} to a {cls}" | |
27 | + .format(typ=type(other).__name__, | |
28 | +cls=type(self).__name__)) | |
29 | + | |
30 | +def _sub_datelike(self, other): | |
31 | +assert other is not NaT | |
32 | +raise TypeError("cannot subtract a datelike from a {cls}" | |
33 | + .format(cls=type(self).__name__)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -696,20 +696,6 @@ def _add_nat(self): | ||
696 | 696 | # and datetime dtypes |
697 | 697 | return self._nat_new(box=True) |
698 | 698 | |
699 | -def _sub_nat(self): | |
700 | -"""Subtract pd.NaT from self""" | |
701 | -# GH#19124 Timedelta - datetime is not in general well-defined. | |
702 | -# We make an exception for pd.NaT, which in this case quacks | |
703 | -# like a timedelta. | |
704 | -# For datetime64 dtypes by convention we treat NaT as a datetime, so | |
705 | -# this subtraction returns a timedelta64 dtype. | |
706 | -# For period dtype, timedelta64 is a close-enough return dtype. | |
707 | -result = self._nat_new(box=False) | |
708 | -return result.view('timedelta64[ns]') | |
709 | - | |
710 | -def _sub_period(self, other): | |
711 | -return NotImplemented | |
712 | - | |
713 | 699 | def _sub_period_array(self, other): |
714 | 700 | """ |
715 | 701 | Subtract one PeriodIndex from another. This is only valid if they |
@@ -745,9 +731,6 @@ def _sub_period_array(self, other): | ||
745 | 731 | new_values[mask] = NaT |
746 | 732 | return new_values |
747 | 733 | |
748 | -def _add_offset(self, offset): | |
749 | -raise com.AbstractMethodError(self) | |
750 | - | |
751 | 734 | def _addsub_offset_array(self, other, op): |
752 | 735 | """ |
753 | 736 | Add or subtract array-like of DateOffset objects |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -743,10 +743,6 @@ def _add_delta(self, other): | ||
743 | 743 | ordinal_delta = self._maybe_convert_timedelta(other) |
744 | 744 | return self.shift(ordinal_delta) |
745 | 745 | |
746 | -def _sub_datelike(self, other): | |
747 | -assert other is not tslib.NaT | |
748 | -return NotImplemented | |
749 | - | |
750 | 746 | def _sub_period(self, other): |
751 | 747 | # If the operation is well-defined, we return an object-Index |
752 | 748 | # of DateOffsets. Null entries are filled with pd.NaT |
@@ -761,6 +757,7 @@ def _sub_period(self, other): | ||
761 | 757 | if self.hasnans: |
762 | 758 | new_data[self._isnan] = tslib.NaT |
763 | 759 | |
760 | +# TODO: Should name=self.name be passed here? | |
764 | 761 | return Index(new_data) |
765 | 762 | |
766 | 763 | def shift(self, n): |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -349,12 +349,6 @@ def _maybe_update_attributes(self, attrs): | ||
349 | 349 | attrs['freq'] = 'infer' |
350 | 350 | return attrs |
351 | 351 | |
352 | -def _add_offset(self, other): | |
353 | -assert not isinstance(other, Tick) | |
354 | -raise TypeError("cannot add the type {typ} to a {cls}" | |
355 | - .format(typ=type(other).__name__, | |
356 | -cls=type(self).__name__)) | |
357 | - | |
358 | 352 | def _add_delta(self, delta): |
359 | 353 | """ |
360 | 354 | Add a timedelta-like, Tick, or TimedeltaIndex-like object |
@@ -430,11 +424,6 @@ def _add_datelike(self, other): | ||
430 | 424 | result = self._maybe_mask_results(result, fill_value=iNaT) |
431 | 425 | return DatetimeIndex(result) |
432 | 426 | |
433 | -def _sub_datelike(self, other): | |
434 | -assert other is not NaT | |
435 | -raise TypeError("cannot subtract a datelike from a {cls}" | |
436 | - .format(cls=type(self).__name__)) | |
437 | - | |
438 | 427 | def _addsub_offset_array(self, other, op): |
439 | 428 | # Add or subtract Array-like of DateOffset objects |
440 | 429 | try: |