-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
delegate (most) datetimelike Series arithmetic ops to DatetimeIndex #18817
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 all commits
8fb1a0d
6d7c6fb
dd88a2c
d86af39
bbea697
99e6cc6
61f22ba
cf677a1
7be6a97
ab75018
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
|
||
import numpy as np | ||
from pandas.core.dtypes.common import ( | ||
is_integer, is_float, | ||
is_integer, is_float, is_integer_dtype, | ||
is_bool_dtype, _ensure_int64, | ||
is_scalar, is_dtype_equal, | ||
is_list_like, is_timedelta64_dtype) | ||
|
@@ -650,6 +650,7 @@ def _add_datetimelike_methods(cls): | |
def __add__(self, other): | ||
from pandas.core.index import Index | ||
from pandas.core.indexes.timedeltas import TimedeltaIndex | ||
from pandas.core.indexes.datetimes import DatetimeIndex | ||
from pandas.tseries.offsets import DateOffset | ||
if is_timedelta64_dtype(other): | ||
return self._add_delta(other) | ||
|
@@ -664,6 +665,12 @@ def __add__(self, other): | |
return self.shift(other) | ||
elif isinstance(other, (Index, datetime, np.datetime64)): | ||
return self._add_datelike(other) | ||
elif (isinstance(self, DatetimeIndex) and | ||
isinstance(other, np.ndarray) and other.size == 1 and | ||
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. huh? I believe we already check is_integer_dtype(other). this check is much too specific 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. Under the status quo adding I'm open to ideas for how to make this less hacky. 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. An alternative would be to -- for the time being -- check for this case in the Series op and avoid dispatching to |
||
is_integer_dtype(other)): | ||
# TODO: Should this be allowed if self.freq is not None? | ||
raise TypeError("cannot add {cls} and {typ}" | ||
.format(cls=type(cls), typ=type(other))) | ||
else: # pragma: no cover | ||
return NotImplemented | ||
cls.__add__ = __add__ | ||
|
@@ -695,6 +702,12 @@ def __sub__(self, other): | |
return self._sub_datelike(other) | ||
elif isinstance(other, Period): | ||
return self._sub_period(other) | ||
elif (isinstance(self, DatetimeIndex) and | ||
isinstance(other, np.ndarray) and other.size == 1 and | ||
is_integer_dtype(other)): | ||
# TODO: Should this be allowed if self.freq is not None? | ||
raise TypeError("cannot add {cls} and {typ}" | ||
.format(cls=type(cls), typ=type(other))) | ||
else: # pragma: no cover | ||
return NotImplemented | ||
cls.__sub__ = __sub__ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,7 +107,7 @@ def test_shift(self): | |
# incompat tz | ||
s2 = Series(date_range('2000-01-01 09:00:00', periods=5, | ||
tz='CET'), name='foo') | ||
pytest.raises(ValueError, lambda: s - s2) | ||
pytest.raises(TypeError, lambda: s - s2) | ||
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. this is 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. as in needs to be noted in whatsnew or needs to be avoided? |
||
|
||
def test_shift2(self): | ||
ts = Series(np.random.randn(5), | ||
|
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.
move to 0.23.0