Skip to content

Commit a34be41

Browse files
mturzanskaalanbato
authored andcommitted
ERR: Clarify exceptions for invalid datetimelike operations (pandas-dev#17772)
1 parent d3cdb97 commit a34be41

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

pandas/core/indexes/datetimelike.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,9 @@ def _convert_scalar_indexer(self, key, kind=None):
621621
._convert_scalar_indexer(key, kind=kind))
622622

623623
def _add_datelike(self, other):
624-
raise AbstractMethodError(self)
624+
raise TypeError("cannot add {0} and {1}"
625+
.format(type(self).__name__,
626+
type(other).__name__))
625627

626628
def _sub_datelike(self, other):
627629
raise AbstractMethodError(self)
@@ -647,16 +649,13 @@ def __add__(self, other):
647649
return other._add_delta(self)
648650
raise TypeError("cannot add TimedeltaIndex and {typ}"
649651
.format(typ=type(other)))
650-
elif isinstance(other, Index):
651-
raise TypeError("cannot add {typ1} and {typ2}"
652-
.format(typ1=type(self).__name__,
653-
typ2=type(other).__name__))
654652
elif isinstance(other, (DateOffset, timedelta, np.timedelta64,
655653
Timedelta)):
656654
return self._add_delta(other)
657655
elif is_integer(other):
658656
return self.shift(other)
659-
elif isinstance(other, (Timestamp, datetime)):
657+
elif isinstance(other, (Index, Timestamp, datetime,
658+
np.datetime64)):
660659
return self._add_datelike(other)
661660
else: # pragma: no cover
662661
return NotImplemented

pandas/core/indexes/datetimes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,9 @@ def _add_datelike(self, other):
754754
# adding a timedeltaindex to a datetimelike
755755
if other is libts.NaT:
756756
return self._nat_new(box=True)
757-
raise TypeError("cannot add a datelike to a DatetimeIndex")
757+
raise TypeError("cannot add {0} and {1}"
758+
.format(type(self).__name__,
759+
type(other).__name__))
758760

759761
def _sub_datelike(self, other):
760762
# subtract a datetime from myself, yielding a TimedeltaIndex

pandas/tests/indexes/datetimes/test_ops.py

+39-19
Original file line numberDiff line numberDiff line change
@@ -433,31 +433,51 @@ def test_add_iadd(self):
433433
tm.assert_index_equal(rng, expected)
434434

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

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

443-
def test_add_dti_dti(self):
444-
# previously performed setop (deprecated in 0.16.0), now raises
445-
# TypeError (GH14164)
446-
447-
dti = date_range('20130101', periods=3)
448-
dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern')
449-
450-
with pytest.raises(TypeError):
451-
dti + dti
452-
453-
with pytest.raises(TypeError):
454-
dti_tz + dti_tz
455-
456-
with pytest.raises(TypeError):
457-
dti_tz + dti
458-
459-
with pytest.raises(TypeError):
460-
dti + dti_tz
443+
@pytest.mark.parametrize('addend', [
444+
datetime(2011, 1, 1),
445+
DatetimeIndex(['2011-01-01', '2011-01-02']),
446+
DatetimeIndex(['2011-01-01', '2011-01-02'])
447+
.tz_localize('US/Eastern'),
448+
np.datetime64('2011-01-01'),
449+
Timestamp('2011-01-01'),
450+
])
451+
def test_add_datetimelike_and_dti(self, addend):
452+
# issue #9631
453+
454+
dti = DatetimeIndex(['2011-01-01', '2011-01-02'])
455+
msg = 'cannot add DatetimeIndex and {0}'.format(
456+
type(addend).__name__)
457+
with tm.assert_raises_regex(TypeError, msg):
458+
dti + addend
459+
with tm.assert_raises_regex(TypeError, msg):
460+
addend + dti
461+
462+
@pytest.mark.parametrize('addend', [
463+
datetime(2011, 1, 1),
464+
DatetimeIndex(['2011-01-01', '2011-01-02']),
465+
DatetimeIndex(['2011-01-01', '2011-01-02'])
466+
.tz_localize('US/Eastern'),
467+
np.datetime64('2011-01-01'),
468+
Timestamp('2011-01-01'),
469+
])
470+
def test_add_datetimelike_and_dti_tz(self, addend):
471+
# issue #9631
472+
473+
dti_tz = DatetimeIndex(['2011-01-01', '2011-01-02']) \
474+
.tz_localize('US/Eastern')
475+
msg = 'cannot add DatetimeIndex and {0}'.format(
476+
type(addend).__name__)
477+
with tm.assert_raises_regex(TypeError, msg):
478+
dti_tz + addend
479+
with tm.assert_raises_regex(TypeError, msg):
480+
addend + dti_tz
461481

462482
def test_difference(self):
463483
for tz in self.tz:

0 commit comments

Comments
 (0)