diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b983117478c61..415255cdbad06 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2325,7 +2325,10 @@ def __sub__(self, other): return Index(np.array(self) - other) def __rsub__(self, other): - return Index(other - np.array(self)) + # wrap Series to ensure we pin name correctly + from pandas import Series + + return Index(other - Series(self)) def __and__(self, other): return self.intersection(other) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index a3bfb2e10bb66..523ba5d42a69c 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -74,8 +74,9 @@ def masked_arith_op(x, y, op): result[mask] = op(xrav[mask], yrav[mask]) else: - assert is_scalar(y), type(y) - assert isinstance(x, np.ndarray), type(x) + if not is_scalar(y): + raise TypeError(type(y)) + # mask is only meaningful for x result = np.empty(x.size, dtype=x.dtype) mask = notna(xrav) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 33a5d45df3885..6d6b85a1e81e1 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -1378,8 +1378,12 @@ def test_td64arr_add_offset_array(self, box): @pytest.mark.parametrize( "names", [(None, None, None), ("foo", "bar", None), ("foo", "foo", "foo")] ) - def test_td64arr_sub_offset_index(self, names, box): + def test_td64arr_sub_offset_index(self, names, box_with_array): # GH#18824, GH#19744 + box = box_with_array + xbox = box if box is not tm.to_array else pd.Index + exname = names[2] if box is not tm.to_array else names[1] + if box is pd.DataFrame and names[1] == "bar": pytest.skip( "Name propagation for DataFrame does not behave like " @@ -1390,11 +1394,11 @@ def test_td64arr_sub_offset_index(self, names, box): other = pd.Index([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)], name=names[1]) expected = TimedeltaIndex( - [tdi[n] - other[n] for n in range(len(tdi))], freq="infer", name=names[2] + [tdi[n] - other[n] for n in range(len(tdi))], freq="infer", name=exname ) tdi = tm.box_expected(tdi, box) - expected = tm.box_expected(expected, box) + expected = tm.box_expected(expected, xbox) # The DataFrame operation is transposed and so operates as separate # scalar operations, which do not issue a PerformanceWarning