-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
cleanup order of operations kludges #19895
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,30 +59,28 @@ def _td_index_cmp(opname, cls): | |
nat_result = True if opname == '__ne__' else False | ||
|
||
def wrapper(self, other): | ||
msg = "cannot compare a TimedeltaIndex with type {0}" | ||
msg = "cannot compare a {cls} with type {typ}" | ||
func = getattr(super(TimedeltaIndex, self), opname) | ||
if _is_convertible_to_td(other) or other is NaT: | ||
try: | ||
other = _to_m8(other) | ||
except ValueError: | ||
# failed to parse as timedelta | ||
raise TypeError(msg.format(type(other))) | ||
raise TypeError(msg.format(cls=type(self).__name__, | ||
typ=type(other).__name__)) | ||
result = func(other) | ||
if isna(other): | ||
result.fill(nat_result) | ||
else: | ||
if not is_list_like(other): | ||
raise TypeError(msg.format(type(other))) | ||
|
||
elif not is_list_like(other): | ||
raise TypeError(msg.format(cls=type(self).__name__, | ||
typ=type(other).__name__)) | ||
else: | ||
other = TimedeltaIndex(other).values | ||
result = func(other) | ||
result = com._values_from_object(result) | ||
|
||
if isinstance(other, Index): | ||
o_mask = other.values.view('i8') == iNaT | ||
else: | ||
o_mask = other.view('i8') == iNaT | ||
|
||
o_mask = np.array(isna(other)) | ||
if o_mask.any(): | ||
result[o_mask] = nat_result | ||
|
||
|
@@ -416,9 +414,15 @@ def _evaluate_with_timedelta_like(self, other, op): | |
def _add_datelike(self, other): | ||
# adding a timedeltaindex to a datetimelike | ||
from pandas import Timestamp, DatetimeIndex | ||
if isinstance(other, np.ndarray): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
other = DatetimeIndex(other) | ||
|
||
if other is NaT: | ||
# GH#19124 pd.NaT is treated like a timedelta | ||
return self._nat_new() | ||
elif isinstance(other, DatetimeIndex): | ||
# defer to implementation in DatetimeIndex | ||
return other + self | ||
else: | ||
other = Timestamp(other) | ||
i8 = self.asi8 | ||
|
@@ -434,7 +438,8 @@ def _sub_datelike(self, other): | |
if other is NaT: | ||
return self._nat_new() | ||
else: | ||
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 | ||
|
@@ -962,8 +967,7 @@ def _is_convertible_to_index(other): | |
|
||
|
||
def _is_convertible_to_td(key): | ||
# TODO: Not all DateOffset objects are convertible to Timedelta | ||
return isinstance(key, (DateOffset, timedelta, Timedelta, | ||
return isinstance(key, (Tick, timedelta, | ||
np.timedelta64, compat.string_types)) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -792,7 +792,7 @@ def test_addition_ops(self): | |
pytest.raises(ValueError, lambda: tdi[0:1] + dti) | ||
|
||
# random indexes | ||
pytest.raises(TypeError, lambda: tdi + Int64Index([1, 2, 3])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this an api change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if it counts as api change or bugfix. The error currently being raises is incorrect. |
||
pytest.raises(NullFrequencyError, lambda: tdi + Int64Index([1, 2, 3])) | ||
|
||
# this is a union! | ||
# pytest.raises(TypeError, lambda : Int64Index([1,2,3]) + tdi) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't you do this in 873, and just always wrap it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.