Skip to content

Commit aebc892

Browse files
comments + lint
1 parent 4c41a76 commit aebc892

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

pandas/tseries/base.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Base and utility classes for tseries type pandas objects.
33
"""
44

5-
import warnings
65
from datetime import datetime, timedelta
76

87
from pandas import compat
@@ -645,17 +644,22 @@ def __add__(self, other):
645644

646645
def __sub__(self, other):
647646
from pandas.core.index import Index
647+
from pandas.tseries.index import DatetimeIndex
648648
from pandas.tseries.tdi import TimedeltaIndex
649649
from pandas.tseries.offsets import DateOffset
650650
if isinstance(other, TimedeltaIndex):
651651
return self._add_delta(-other)
652652
elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
653653
if not isinstance(other, TimedeltaIndex):
654654
raise TypeError("cannot subtract TimedeltaIndex and {typ}"
655-
.format(typ=type(other)))
655+
.format(typ=type(other).__name__))
656656
return self._add_delta(-other)
657-
elif isinstance(other, Index):
657+
elif isinstance(other, DatetimeIndex):
658658
return self._sub_datelike(other)
659+
elif isinstance(other, Index):
660+
raise TypeError("cannot add {typ1} and {typ2}"
661+
.format(typ1=type(self).__name__,
662+
typ2=type(other).__name__))
659663
elif isinstance(other, (DateOffset, timedelta, np.timedelta64,
660664
tslib.Timedelta)):
661665
return self._add_delta(-other)

pandas/tseries/index.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -748,14 +748,18 @@ def _sub_datelike(self, other):
748748
else:
749749
i8 = self.asi8
750750
result = i8 - other.value
751-
result = self._maybe_mask_results(result, fill_value=tslib.iNaT)
751+
result = self._maybe_mask_results(result,
752+
fill_value=tslib.iNaT)
752753
else:
753754
raise TypeError("cannot subtract DatetimeIndex and {typ}"
754755
.format(typ=type(other).__name__))
755756
return TimedeltaIndex(result, name=self.name, copy=False)
756757

757758
def _sub_datelike_dti(self, other):
758759
"""subtraction of two DatetimeIndexes"""
760+
if not len(self) == len(other):
761+
raise ValueError("cannot add indices of unequal length")
762+
759763
self_i8 = self.asi8
760764
other_i8 = other.asi8
761765
new_values = self_i8 - other_i8

pandas/tseries/tests/test_base.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def test_add_iadd(self):
400400
(rng2, other2, expected2),
401401
(rng3, other3, expected3)]:
402402
# previously performed setop (deprecated in 0.16.0), now
403-
# raises TypeError (GH..)
403+
# raises TypeError (GH14164)
404404
with tm.assertRaises(TypeError):
405405
rng + other
406406

@@ -440,7 +440,7 @@ def test_add_iadd(self):
440440

441441
def test_add_dti_dti(self):
442442
# previously performed setop (deprecated in 0.16.0), now raises
443-
# TypeError (GH..)
443+
# TypeError (GH14164)
444444

445445
dti = date_range('20130101', periods=3)
446446
dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern')
@@ -480,27 +480,17 @@ def test_diff(self):
480480

481481
def test_sub_isub(self):
482482
for tz in self.tz:
483-
rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
484-
other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz)
485-
expected1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
486-
487-
rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
488-
other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz)
489-
expected2 = pd.date_range('1/1/2000', freq='D', periods=3, tz=tz)
490-
491-
rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
492-
other3 = pd.DatetimeIndex([], tz=tz)
493-
expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
494483

495484
# offset
496485
offsets = [pd.offsets.Hour(2), timedelta(hours=2),
497486
np.timedelta64(2, 'h'), Timedelta(hours=2)]
498487

499488
for delta in offsets:
500489
rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz)
501-
result = rng - delta
502490
expected = pd.date_range('1999-12-31 22:00',
503491
'2000-01-31 22:00', tz=tz)
492+
493+
result = rng - delta
504494
tm.assert_index_equal(result, expected)
505495
rng -= delta
506496
tm.assert_index_equal(rng, expected)
@@ -539,6 +529,18 @@ def test_sub_dti_dti(self):
539529
with tm.assertRaises(TypeError):
540530
dti_tz - dti_tz2
541531

532+
# different length raises ValueError
533+
dti2 = date_range('20130101', periods=4)
534+
with tm.assertRaises(ValueError):
535+
dti - dti2
536+
537+
# NaN propagation
538+
dti1 = DatetimeIndex(['2012-01-01', np.nan, '2012-01-03'])
539+
dti2 = DatetimeIndex(['2012-01-02', '2012-01-03', np.nan])
540+
expected = TimedeltaIndex(['1 days', np.nan, np.nan])
541+
result = dti2 - dti1
542+
tm.assert_index_equal(result, expected)
543+
542544
def test_sub_period(self):
543545
# GH 13078
544546
# not supported, check TypeError

0 commit comments

Comments
 (0)