Skip to content

ERR: Clarify exceptions for invalid datetimelike operations #17772

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 8 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,9 @@ def _convert_scalar_indexer(self, key, kind=None):
._convert_scalar_indexer(key, kind=kind))

def _add_datelike(self, other):
raise AbstractMethodError(self)
raise TypeError("cannot add {0} and {1}"
.format(type(self).__name__,
type(other).__name__))

def _sub_datelike(self, other):
raise AbstractMethodError(self)
Expand All @@ -647,16 +649,13 @@ def __add__(self, other):
return other._add_delta(self)
raise TypeError("cannot add TimedeltaIndex and {typ}"
.format(typ=type(other)))
elif isinstance(other, Index):
raise TypeError("cannot add {typ1} and {typ2}"
.format(typ1=type(self).__name__,
typ2=type(other).__name__))
elif isinstance(other, (DateOffset, timedelta, np.timedelta64,
Timedelta)):
return self._add_delta(other)
elif is_integer(other):
return self.shift(other)
elif isinstance(other, (Timestamp, datetime)):
elif isinstance(other, (Index, Timestamp, datetime,
np.datetime64)):
return self._add_datelike(other)
else: # pragma: no cover
return NotImplemented
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,9 @@ def _add_datelike(self, other):
# adding a timedeltaindex to a datetimelike
if other is libts.NaT:
return self._nat_new(box=True)
raise TypeError("cannot add a datelike to a DatetimeIndex")
raise TypeError("cannot add {0} and {1}"
.format(type(self).__name__,
type(other).__name__))

def _sub_datelike(self, other):
# subtract a datetime from myself, yielding a TimedeltaIndex
Expand Down
58 changes: 39 additions & 19 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,31 +434,51 @@ def test_add_iadd(self):
tm.assert_index_equal(rng, expected)

idx = DatetimeIndex(['2011-01-01', '2011-01-02'])
msg = "cannot add a datelike to a DatetimeIndex"
msg = "cannot add DatetimeIndex and Timestamp"
with tm.assert_raises_regex(TypeError, msg):
idx + Timestamp('2011-01-01')

with tm.assert_raises_regex(TypeError, msg):
Timestamp('2011-01-01') + idx

def test_add_dti_dti(self):
# previously performed setop (deprecated in 0.16.0), now raises
# TypeError (GH14164)

dti = date_range('20130101', periods=3)
dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern')

with pytest.raises(TypeError):
dti + dti

with pytest.raises(TypeError):
dti_tz + dti_tz

with pytest.raises(TypeError):
dti_tz + dti

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I think you removed the dti_tz + dti_tz tests (IOW a tz-aware interacting with a non-tzaware). these can be a separate test function. also add a datetime scalar addend.

with pytest.raises(TypeError):
dti + dti_tz
@pytest.mark.parametrize('addend', [
datetime(2011, 1, 1),
DatetimeIndex(['2011-01-01', '2011-01-02']),
DatetimeIndex(['2011-01-01', '2011-01-02'])
.tz_localize('US/Eastern'),
np.datetime64('2011-01-01'),
Timestamp('2011-01-01'),
])
def test_add_datetimelike_and_dti(self, addend):
# issue #9631

dti = DatetimeIndex(['2011-01-01', '2011-01-02'])
msg = 'cannot add DatetimeIndex and {0}'.format(
type(addend).__name__)
with tm.assert_raises_regex(TypeError, msg):
dti + addend
with tm.assert_raises_regex(TypeError, msg):
addend + dti

@pytest.mark.parametrize('addend', [
datetime(2011, 1, 1),
DatetimeIndex(['2011-01-01', '2011-01-02']),
DatetimeIndex(['2011-01-01', '2011-01-02'])
.tz_localize('US/Eastern'),
np.datetime64('2011-01-01'),
Timestamp('2011-01-01'),
])
def test_add_datetimelike_and_dti_tz(self, addend):
# issue #9631

dti_tz = DatetimeIndex(['2011-01-01', '2011-01-02']) \
.tz_localize('US/Eastern')
msg = 'cannot add DatetimeIndex and {0}'.format(
type(addend).__name__)
with tm.assert_raises_regex(TypeError, msg):
dti_tz + addend
with tm.assert_raises_regex(TypeError, msg):
addend + dti_tz

def test_difference(self):
for tz in self.tz:
Expand Down