Closed
Description
Curious about why Index.__add__
tries does the natural thing but Index.__sub__
raises TypeError
.
def __add__(self, other):
return Index(np.array(self) + other)
def __radd__(self, other):
return Index(other + np.array(self))
__iadd__ = __add__
def __sub__(self, other):
raise TypeError("cannot perform __sub__ with this index type: "
"{typ}".format(typ=type(self)))
Activity
jreback commentedon Jan 24, 2018
this is why comments are important!
add is concat
eg
Index(list(‘abc’)) + ‘d’
sub doesn’t make sense
jbrockmendel commentedon Jan 24, 2018
That seems pretty string-specific. Shouldn't the object-dtype try to broadcast the scalar operation?
The motivation here is in https://github.com/pandas-dev/pandas/blob/master/pandas/core/indexes/datetimes.py#L933
where
_add_offset_array
can useself.astype('O') + np.array(other)
but_sub_offset_array
has to useself.__class__(self.astype('O').values - np.array(other))