Skip to content

Commit 2b13605

Browse files
jbrockmendeljreback
authored andcommitted
Move Unchanged arith methods to EA mixins (#21712)
1 parent 0cb0886 commit 2b13605

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

pandas/core/arrays/datetimelike.py

+12
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,15 @@ def _add_delta_tdi(self, other):
167167
mask = (self._isnan) | (other._isnan)
168168
new_values[mask] = iNaT
169169
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]')

pandas/core/arrays/period.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
from pandas._libs.tslib import NaT
34
from pandas._libs.tslibs.period import Period
45

56
from pandas.util._decorators import cache_readonly
@@ -26,3 +27,10 @@ def _ndarray_values(self):
2627
@property
2728
def asi8(self):
2829
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

pandas/core/arrays/timedelta.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# -*- coding: utf-8 -*-
22

3-
from pandas._libs.tslib import Timedelta
3+
from pandas._libs.tslib import Timedelta, NaT
44

55
from pandas.core.dtypes.common import _TD_DTYPE
66

7+
from pandas.tseries.offsets import Tick
8+
79
from .datetimelike import DatetimeLikeArrayMixin
810

911

@@ -15,3 +17,17 @@ def _box_func(self):
1517
@property
1618
def dtype(self):
1719
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__))

pandas/core/indexes/datetimelike.py

-17
Original file line numberDiff line numberDiff line change
@@ -696,20 +696,6 @@ def _add_nat(self):
696696
# and datetime dtypes
697697
return self._nat_new(box=True)
698698

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-
713699
def _sub_period_array(self, other):
714700
"""
715701
Subtract one PeriodIndex from another. This is only valid if they
@@ -745,9 +731,6 @@ def _sub_period_array(self, other):
745731
new_values[mask] = NaT
746732
return new_values
747733

748-
def _add_offset(self, offset):
749-
raise com.AbstractMethodError(self)
750-
751734
def _addsub_offset_array(self, other, op):
752735
"""
753736
Add or subtract array-like of DateOffset objects

pandas/core/indexes/period.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,6 @@ def _add_delta(self, other):
743743
ordinal_delta = self._maybe_convert_timedelta(other)
744744
return self.shift(ordinal_delta)
745745

746-
def _sub_datelike(self, other):
747-
assert other is not tslib.NaT
748-
return NotImplemented
749-
750746
def _sub_period(self, other):
751747
# If the operation is well-defined, we return an object-Index
752748
# of DateOffsets. Null entries are filled with pd.NaT
@@ -761,6 +757,7 @@ def _sub_period(self, other):
761757
if self.hasnans:
762758
new_data[self._isnan] = tslib.NaT
763759

760+
# TODO: Should name=self.name be passed here?
764761
return Index(new_data)
765762

766763
def shift(self, n):

pandas/core/indexes/timedeltas.py

-11
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,6 @@ def _maybe_update_attributes(self, attrs):
349349
attrs['freq'] = 'infer'
350350
return attrs
351351

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-
358352
def _add_delta(self, delta):
359353
"""
360354
Add a timedelta-like, Tick, or TimedeltaIndex-like object
@@ -430,11 +424,6 @@ def _add_datelike(self, other):
430424
result = self._maybe_mask_results(result, fill_value=iNaT)
431425
return DatetimeIndex(result)
432426

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-
438427
def _addsub_offset_array(self, other, op):
439428
# Add or subtract Array-like of DateOffset objects
440429
try:

0 commit comments

Comments
 (0)