Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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
Expand Down