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 5 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
33 changes: 28 additions & 5 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,30 +434,53 @@ 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_ts(self):
dti = DatetimeIndex(['2011-01-01', '2011-01-02'])
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment

ts = Timestamp('2011-01-01')
msg = 'cannot add DatetimeIndex and Timestamp'
Copy link
Contributor

Choose a reason for hiding this comment

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

can you combine these into a single tests and use parametrize for the args


with tm.assert_raises_regex(TypeError, msg):
dti + ts

with tm.assert_raises_regex(TypeError, msg):
ts + dti

def test_add_dti_dt64(self):
dti = DatetimeIndex(['2011-01-01', '2011-01-02'])
dt64 = np.datetime64('2005-02-25')
msg = 'cannot add DatetimeIndex and datetime64'

with tm.assert_raises_regex(TypeError, msg):
dti + dt64

with tm.assert_raises_regex(TypeError, msg):
dt64 + dti

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')
msg = 'cannot add DatetimeIndex and DatetimeIndex'
Copy link
Contributor

Choose a reason for hiding this comment

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

can u parametrize this as well


with pytest.raises(TypeError):
with tm.assert_raises_regex(TypeError, msg):
dti + dti

with pytest.raises(TypeError):
with tm.assert_raises_regex(TypeError, msg):
dti_tz + dti_tz

with pytest.raises(TypeError):
with tm.assert_raises_regex(TypeError, msg):
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):
with tm.assert_raises_regex(TypeError, msg):
dti + dti_tz

def test_difference(self):
Expand Down