From b08b9125a1b410a4882ba5ce068d7e53531c6cc2 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 2 Jul 2018 19:01:48 -0700 Subject: [PATCH 1/3] remove duplicated methods --- pandas/core/indexes/datetimelike.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 328c51aae1807..b78162811853e 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -707,9 +707,6 @@ def _sub_nat(self): 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 @@ -745,9 +742,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 From 8158155e210bcc846493c30655138effa36299da Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 2 Jul 2018 19:20:49 -0700 Subject: [PATCH 2/3] move easiest of arith methods --- pandas/core/arrays/datetimelike.py | 12 ++++++++++++ pandas/core/arrays/timedelta.py | 18 +++++++++++++++++- pandas/core/indexes/datetimelike.py | 11 ----------- pandas/core/indexes/period.py | 1 + pandas/core/indexes/timedeltas.py | 11 ----------- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 180417ce5e49c..be3f94201f103 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -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]') diff --git a/pandas/core/arrays/timedelta.py b/pandas/core/arrays/timedelta.py index 487858c49b66a..85c5bdc566ff1 100644 --- a/pandas/core/arrays/timedelta.py +++ b/pandas/core/arrays/timedelta.py @@ -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 @@ -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__)) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index b78162811853e..47e7022c99a21 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -696,17 +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_array(self, other): """ Subtract one PeriodIndex from another. This is only valid if they diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 1257266025c03..2d36679e5f6a9 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -763,6 +763,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): diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 8b62d9aa631e9..80b9bf92df14a 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -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 @@ -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: From 32e4269509468f3d9d692d0284263ac31564a80a Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 2 Jul 2018 19:23:39 -0700 Subject: [PATCH 3/3] move sub_datelike --- pandas/core/arrays/period.py | 8 ++++++++ pandas/core/indexes/period.py | 4 ---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 1158bae748f0a..03c0128c67c99 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -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 @@ -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 diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 2d36679e5f6a9..a8c12ea48784d 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -745,10 +745,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