Skip to content

Commit be08902

Browse files
jbrockmendelTomAugspurger
authored andcommitted
BUG: TimedeltaArray - Index result.name (#27962)
* BUG: TimedeltaArray - Index result.name * change assert to TypeErrror
1 parent 325dd68 commit be08902

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

pandas/core/indexes/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,10 @@ def __sub__(self, other):
23252325
return Index(np.array(self) - other)
23262326

23272327
def __rsub__(self, other):
2328-
return Index(other - np.array(self))
2328+
# wrap Series to ensure we pin name correctly
2329+
from pandas import Series
2330+
2331+
return Index(other - Series(self))
23292332

23302333
def __and__(self, other):
23312334
return self.intersection(other)

pandas/core/ops/array_ops.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ def masked_arith_op(x, y, op):
7474
result[mask] = op(xrav[mask], yrav[mask])
7575

7676
else:
77-
assert is_scalar(y), type(y)
78-
assert isinstance(x, np.ndarray), type(x)
77+
if not is_scalar(y):
78+
raise TypeError(type(y))
79+
7980
# mask is only meaningful for x
8081
result = np.empty(x.size, dtype=x.dtype)
8182
mask = notna(xrav)

pandas/tests/arithmetic/test_timedelta64.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1378,8 +1378,12 @@ def test_td64arr_add_offset_array(self, box):
13781378
@pytest.mark.parametrize(
13791379
"names", [(None, None, None), ("foo", "bar", None), ("foo", "foo", "foo")]
13801380
)
1381-
def test_td64arr_sub_offset_index(self, names, box):
1381+
def test_td64arr_sub_offset_index(self, names, box_with_array):
13821382
# GH#18824, GH#19744
1383+
box = box_with_array
1384+
xbox = box if box is not tm.to_array else pd.Index
1385+
exname = names[2] if box is not tm.to_array else names[1]
1386+
13831387
if box is pd.DataFrame and names[1] == "bar":
13841388
pytest.skip(
13851389
"Name propagation for DataFrame does not behave like "
@@ -1390,11 +1394,11 @@ def test_td64arr_sub_offset_index(self, names, box):
13901394
other = pd.Index([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)], name=names[1])
13911395

13921396
expected = TimedeltaIndex(
1393-
[tdi[n] - other[n] for n in range(len(tdi))], freq="infer", name=names[2]
1397+
[tdi[n] - other[n] for n in range(len(tdi))], freq="infer", name=exname
13941398
)
13951399

13961400
tdi = tm.box_expected(tdi, box)
1397-
expected = tm.box_expected(expected, box)
1401+
expected = tm.box_expected(expected, xbox)
13981402

13991403
# The DataFrame operation is transposed and so operates as separate
14001404
# scalar operations, which do not issue a PerformanceWarning

0 commit comments

Comments
 (0)