Skip to content

handle NaT add/sub in one place #19903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' of https://github.com/pandas-dev/pandas into id…
…x_nat_ops
  • Loading branch information
jbrockmendel committed Feb 26, 2018
commit ef76acb630a4520de7e2a63f83fb54574dedeef0
19 changes: 11 additions & 8 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,14 @@ def __setstate__(self, state):

def _sub_datelike(self, other):
# subtract a datetime from myself, yielding a ndarray[timedelta64[ns]]
if isinstance(other, DatetimeIndex):
if isinstance(other, (DatetimeIndex, np.ndarray)):
# if other is an ndarray, we assume it is datetime64-dtype
other = DatetimeIndex(other)
# require tz compat
if not self._has_same_tz(other):
raise TypeError("{cls} subtraction must have the same "
"timezones or no timezones"
.format(cls=type(self).__name__))
result = self._sub_datelike_dti(other)
elif isinstance(other, (datetime, np.datetime64)):
assert other is not libts.NaT
Expand All @@ -873,19 +880,15 @@ def _sub_datelike(self, other):
result = self._maybe_mask_results(result,
fill_value=libts.iNaT)
else:
raise TypeError("cannot subtract DatetimeIndex and {typ}"
.format(typ=type(other).__name__))
raise TypeError("cannot subtract {cls} and {typ}"
.format(cls=type(self).__name__,
typ=type(other).__name__))
return result.view('timedelta64[ns]')

def _sub_datelike_dti(self, other):
"""subtraction of two DatetimeIndexes"""
if not len(self) == len(other):
raise ValueError("cannot add indices of unequal length")
elif not self._has_same_tz(other):
# require tz compat
raise TypeError("{cls} subtraction must have the same "
"timezones or no timezones"
.format(cls=type(self).__name__))

self_i8 = self.asi8
other_i8 = other.asi8
Expand Down
23 changes: 15 additions & 8 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,24 @@ def _evaluate_with_timedelta_like(self, other, op):
def _add_datelike(self, other):
# adding a timedeltaindex to a datetimelike
from pandas import Timestamp, DatetimeIndex
assert other is not NaT
other = Timestamp(other)
i8 = self.asi8
result = checked_add_with_arr(i8, other.value,
arr_mask=self._isnan)
result = self._maybe_mask_results(result, fill_value=iNaT)
return DatetimeIndex(result)
if isinstance(other, (DatetimeIndex, np.ndarray)):
# if other is an ndarray, we assume it is datetime64-dtype
# defer to implementation in DatetimeIndex
other = DatetimeIndex(other)
return other + self
else:
assert other is not NaT
other = Timestamp(other)
i8 = self.asi8
result = checked_add_with_arr(i8, other.value,
arr_mask=self._isnan)
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 TimedeltaIndex")
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
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.