Skip to content

Commit

Permalink
Move Unchanged arith methods to EA mixins (#21712)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jul 3, 2018
1 parent 0cb0886 commit 2b13605
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
12 changes: 12 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,15 @@ def _add_delta_tdi(self, other):
mask = (self._isnan) | (other._isnan)
new_values[mask] = iNaT
return new_values.view('i8')

def _sub_nat(self):
"""Subtract pd.NaT from self"""
# GH#19124 Timedelta - datetime is not in general well-defined.
# We make an exception for pd.NaT, which in this case quacks
# like a timedelta.
# For datetime64 dtypes by convention we treat NaT as a datetime, so
# this subtraction returns a timedelta64 dtype.
# For period dtype, timedelta64 is a close-enough return dtype.
result = np.zeros(len(self), dtype=np.int64)
result.fill(iNaT)
return result.view('timedelta64[ns]')
8 changes: 8 additions & 0 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

from pandas._libs.tslib import NaT
from pandas._libs.tslibs.period import Period

from pandas.util._decorators import cache_readonly
Expand All @@ -26,3 +27,10 @@ def _ndarray_values(self):
@property
def asi8(self):
return self._ndarray_values.view('i8')

# ------------------------------------------------------------------
# Arithmetic Methods

def _sub_datelike(self, other):
assert other is not NaT
return NotImplemented
18 changes: 17 additions & 1 deletion pandas/core/arrays/timedelta.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-

from pandas._libs.tslib import Timedelta
from pandas._libs.tslib import Timedelta, NaT

from pandas.core.dtypes.common import _TD_DTYPE

from pandas.tseries.offsets import Tick

from .datetimelike import DatetimeLikeArrayMixin


Expand All @@ -15,3 +17,17 @@ def _box_func(self):
@property
def dtype(self):
return _TD_DTYPE

# ----------------------------------------------------------------
# Arithmetic Methods

def _add_offset(self, other):
assert not isinstance(other, Tick)
raise TypeError("cannot add the type {typ} to a {cls}"
.format(typ=type(other).__name__,
cls=type(self).__name__))

def _sub_datelike(self, other):
assert other is not NaT
raise TypeError("cannot subtract a datelike from a {cls}"
.format(cls=type(self).__name__))
17 changes: 0 additions & 17 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,20 +696,6 @@ def _add_nat(self):
# and datetime dtypes
return self._nat_new(box=True)

def _sub_nat(self):
"""Subtract pd.NaT from self"""
# GH#19124 Timedelta - datetime is not in general well-defined.
# We make an exception for pd.NaT, which in this case quacks
# like a timedelta.
# For datetime64 dtypes by convention we treat NaT as a datetime, so
# this subtraction returns a timedelta64 dtype.
# For period dtype, timedelta64 is a close-enough return dtype.
result = self._nat_new(box=False)
return result.view('timedelta64[ns]')

def _sub_period(self, other):
return NotImplemented

def _sub_period_array(self, other):
"""
Subtract one PeriodIndex from another. This is only valid if they
Expand Down Expand Up @@ -745,9 +731,6 @@ def _sub_period_array(self, other):
new_values[mask] = NaT
return new_values

def _add_offset(self, offset):
raise com.AbstractMethodError(self)

def _addsub_offset_array(self, other, op):
"""
Add or subtract array-like of DateOffset objects
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,6 @@ def _add_delta(self, other):
ordinal_delta = self._maybe_convert_timedelta(other)
return self.shift(ordinal_delta)

def _sub_datelike(self, other):
assert other is not tslib.NaT
return NotImplemented

def _sub_period(self, other):
# If the operation is well-defined, we return an object-Index
# of DateOffsets. Null entries are filled with pd.NaT
Expand All @@ -761,6 +757,7 @@ def _sub_period(self, other):
if self.hasnans:
new_data[self._isnan] = tslib.NaT

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

def shift(self, n):
Expand Down
11 changes: 0 additions & 11 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,6 @@ def _maybe_update_attributes(self, attrs):
attrs['freq'] = 'infer'
return attrs

def _add_offset(self, other):
assert not isinstance(other, Tick)
raise TypeError("cannot add the type {typ} to a {cls}"
.format(typ=type(other).__name__,
cls=type(self).__name__))

def _add_delta(self, delta):
"""
Add a timedelta-like, Tick, or TimedeltaIndex-like object
Expand Down Expand Up @@ -430,11 +424,6 @@ def _add_datelike(self, other):
result = self._maybe_mask_results(result, fill_value=iNaT)
return DatetimeIndex(result)

def _sub_datelike(self, other):
assert other is not NaT
raise TypeError("cannot subtract a datelike from a {cls}"
.format(cls=type(self).__name__))

def _addsub_offset_array(self, other, op):
# Add or subtract Array-like of DateOffset objects
try:
Expand Down

0 comments on commit 2b13605

Please sign in to comment.