37
37
is_period_dtype ,
38
38
is_timedelta64_dtype )
39
39
from pandas .core .dtypes .generic import (
40
- ABCIndex , ABCSeries , ABCPeriodIndex , ABCIndexClass )
40
+ ABCIndex , ABCSeries , ABCDataFrame , ABCPeriodIndex , ABCIndexClass )
41
41
from pandas .core .dtypes .missing import isna
42
42
from pandas .core import common as com , algorithms , ops
43
43
from pandas .core .algorithms import checked_add_with_arr
48
48
from pandas .util ._decorators import Appender , cache_readonly
49
49
import pandas .core .dtypes .concat as _concat
50
50
import pandas .tseries .frequencies as frequencies
51
+ from pandas .tseries .offsets import Tick , DateOffset
51
52
52
53
import pandas .core .indexes .base as ibase
53
54
_index_doc_kwargs = dict (ibase ._index_doc_kwargs )
@@ -666,6 +667,9 @@ def _sub_nat(self):
666
667
def _sub_period (self , other ):
667
668
return NotImplemented
668
669
670
+ def _add_offset (self , offset ):
671
+ raise com .AbstractMethodError (self )
672
+
669
673
def _addsub_offset_array (self , other , op ):
670
674
"""
671
675
Add or subtract array-like of DateOffset objects
@@ -705,14 +709,17 @@ def __add__(self, other):
705
709
from pandas import DateOffset
706
710
707
711
other = lib .item_from_zerodim (other )
708
- if isinstance (other , ABCSeries ):
712
+ if isinstance (other , ( ABCSeries , ABCDataFrame ) ):
709
713
return NotImplemented
710
714
711
715
# scalar others
712
716
elif other is NaT :
713
717
result = self ._add_nat ()
714
- elif isinstance (other , (DateOffset , timedelta , np .timedelta64 )):
718
+ elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
715
719
result = self ._add_delta (other )
720
+ elif isinstance (other , DateOffset ):
721
+ # specifically _not_ a Tick
722
+ result = self ._add_offset (other )
716
723
elif isinstance (other , (datetime , np .datetime64 )):
717
724
result = self ._add_datelike (other )
718
725
elif is_integer (other ):
@@ -733,6 +740,12 @@ def __add__(self, other):
733
740
elif is_integer_dtype (other ) and self .freq is None :
734
741
# GH#19123
735
742
raise NullFrequencyError ("Cannot shift with no freq" )
743
+ elif is_float_dtype (other ):
744
+ # Explicitly catch invalid dtypes
745
+ raise TypeError ("cannot add {dtype}-dtype to {cls}"
746
+ .format (dtype = other .dtype ,
747
+ cls = type (self ).__name__ ))
748
+
736
749
else : # pragma: no cover
737
750
return NotImplemented
738
751
@@ -753,17 +766,20 @@ def __radd__(self, other):
753
766
cls .__radd__ = __radd__
754
767
755
768
def __sub__ (self , other ):
756
- from pandas import Index , DateOffset
769
+ from pandas import Index
757
770
758
771
other = lib .item_from_zerodim (other )
759
- if isinstance (other , ABCSeries ):
772
+ if isinstance (other , ( ABCSeries , ABCDataFrame ) ):
760
773
return NotImplemented
761
774
762
775
# scalar others
763
776
elif other is NaT :
764
777
result = self ._sub_nat ()
765
- elif isinstance (other , (DateOffset , timedelta , np .timedelta64 )):
778
+ elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
766
779
result = self ._add_delta (- other )
780
+ elif isinstance (other , DateOffset ):
781
+ # specifically _not_ a Tick
782
+ result = self ._add_offset (- other )
767
783
elif isinstance (other , (datetime , np .datetime64 )):
768
784
result = self ._sub_datelike (other )
769
785
elif is_integer (other ):
@@ -790,6 +806,12 @@ def __sub__(self, other):
790
806
elif is_integer_dtype (other ) and self .freq is None :
791
807
# GH#19123
792
808
raise NullFrequencyError ("Cannot shift with no freq" )
809
+
810
+ elif is_float_dtype (other ):
811
+ # Explicitly catch invalid dtypes
812
+ raise TypeError ("cannot subtract {dtype}-dtype from {cls}"
813
+ .format (dtype = other .dtype ,
814
+ cls = type (self ).__name__ ))
793
815
else : # pragma: no cover
794
816
return NotImplemented
795
817
0 commit comments