3737 is_period_dtype ,
3838 is_timedelta64_dtype )
3939from pandas .core .dtypes .generic import (
40- ABCIndex , ABCSeries , ABCPeriodIndex , ABCIndexClass )
40+ ABCIndex , ABCSeries , ABCDataFrame , ABCPeriodIndex , ABCIndexClass )
4141from pandas .core .dtypes .missing import isna
4242from pandas .core import common as com , algorithms , ops
4343from pandas .core .algorithms import checked_add_with_arr
4848from pandas .util ._decorators import Appender , cache_readonly
4949import pandas .core .dtypes .concat as _concat
5050import pandas .tseries .frequencies as frequencies
51+ from pandas .tseries .offsets import Tick , DateOffset
5152
5253import pandas .core .indexes .base as ibase
5354_index_doc_kwargs = dict (ibase ._index_doc_kwargs )
@@ -666,6 +667,9 @@ def _sub_nat(self):
666667 def _sub_period (self , other ):
667668 return NotImplemented
668669
670+ def _add_offset (self , offset ):
671+ raise com .AbstractMethodError (self )
672+
669673 def _addsub_offset_array (self , other , op ):
670674 """
671675 Add or subtract array-like of DateOffset objects
@@ -705,14 +709,17 @@ def __add__(self, other):
705709 from pandas import DateOffset
706710
707711 other = lib .item_from_zerodim (other )
708- if isinstance (other , ABCSeries ):
712+ if isinstance (other , ( ABCSeries , ABCDataFrame ) ):
709713 return NotImplemented
710714
711715 # scalar others
712716 elif other is NaT :
713717 result = self ._add_nat ()
714- elif isinstance (other , (DateOffset , timedelta , np .timedelta64 )):
718+ elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
715719 result = self ._add_delta (other )
720+ elif isinstance (other , DateOffset ):
721+ # specifically _not_ a Tick
722+ result = self ._add_offset (other )
716723 elif isinstance (other , (datetime , np .datetime64 )):
717724 result = self ._add_datelike (other )
718725 elif is_integer (other ):
@@ -733,6 +740,12 @@ def __add__(self, other):
733740 elif is_integer_dtype (other ) and self .freq is None :
734741 # GH#19123
735742 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+
736749 else : # pragma: no cover
737750 return NotImplemented
738751
@@ -753,17 +766,20 @@ def __radd__(self, other):
753766 cls .__radd__ = __radd__
754767
755768 def __sub__ (self , other ):
756- from pandas import Index , DateOffset
769+ from pandas import Index
757770
758771 other = lib .item_from_zerodim (other )
759- if isinstance (other , ABCSeries ):
772+ if isinstance (other , ( ABCSeries , ABCDataFrame ) ):
760773 return NotImplemented
761774
762775 # scalar others
763776 elif other is NaT :
764777 result = self ._sub_nat ()
765- elif isinstance (other , (DateOffset , timedelta , np .timedelta64 )):
778+ elif isinstance (other , (Tick , timedelta , np .timedelta64 )):
766779 result = self ._add_delta (- other )
780+ elif isinstance (other , DateOffset ):
781+ # specifically _not_ a Tick
782+ result = self ._add_offset (- other )
767783 elif isinstance (other , (datetime , np .datetime64 )):
768784 result = self ._sub_datelike (other )
769785 elif is_integer (other ):
@@ -790,6 +806,12 @@ def __sub__(self, other):
790806 elif is_integer_dtype (other ) and self .freq is None :
791807 # GH#19123
792808 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__ ))
793815 else : # pragma: no cover
794816 return NotImplemented
795817
0 commit comments