diff --git a/pandas/core/ops/dispatch.py b/pandas/core/ops/dispatch.py index 637f0fa1d52e9..a7dcdd4f9d585 100644 --- a/pandas/core/ops/dispatch.py +++ b/pandas/core/ops/dispatch.py @@ -65,8 +65,12 @@ def should_series_dispatch(left, right, op): ldtype = left.dtypes.iloc[0] rdtype = right.dtypes.iloc[0] - if (is_timedelta64_dtype(ldtype) and is_integer_dtype(rdtype)) or ( - is_timedelta64_dtype(rdtype) and is_integer_dtype(ldtype) + if ( + is_timedelta64_dtype(ldtype) + and (is_integer_dtype(rdtype) or is_object_dtype(rdtype)) + ) or ( + is_timedelta64_dtype(rdtype) + and (is_integer_dtype(ldtype) or is_object_dtype(ldtype)) ): # numpy integer dtypes as timedelta64 dtypes in this scenario return True diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index b2ea6d8b92fdd..e3eec1f781948 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -541,9 +541,10 @@ def test_tda_add_sub_index(self): expected = tdi - tdi tm.assert_index_equal(result, expected) - def test_tda_add_dt64_object_array(self, box_df_fail, tz_naive_fixture): + def test_tda_add_dt64_object_array(self, box_with_array, tz_naive_fixture): # Result should be cast back to DatetimeArray - box = box_df_fail + box = box_with_array + dti = pd.date_range("2016-01-01", periods=3, tz=tz_naive_fixture) dti = dti._with_freq(None) tdi = dti - dti @@ -1396,33 +1397,40 @@ def test_td64arr_sub_offset_array(self, box_with_array): res = tdi - other tm.assert_equal(res, expected) - def test_td64arr_with_offset_series(self, names, box_df_fail): + def test_td64arr_with_offset_series(self, names, box_with_array): # GH#18849 - box = box_df_fail + box = box_with_array box2 = Series if box in [pd.Index, tm.to_array] else box - exname = names[2] if box is not tm.to_array else names[1] + + if box is pd.DataFrame: + # Since we are operating with a DataFrame and a non-DataFrame, + # the non-DataFrame is cast to Series and its name ignored. + exname = names[0] + elif box is tm.to_array: + exname = names[1] + else: + exname = names[2] tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"], name=names[0]) other = Series([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)], name=names[1]) expected_add = Series([tdi[n] + other[n] for n in range(len(tdi))], name=exname) - tdi = tm.box_expected(tdi, box) + obj = tm.box_expected(tdi, box) expected_add = tm.box_expected(expected_add, box2) with tm.assert_produces_warning(PerformanceWarning): - res = tdi + other + res = obj + other tm.assert_equal(res, expected_add) with tm.assert_produces_warning(PerformanceWarning): - res2 = other + tdi + res2 = other + obj tm.assert_equal(res2, expected_add) - # TODO: separate/parametrize add/sub test? expected_sub = Series([tdi[n] - other[n] for n in range(len(tdi))], name=exname) expected_sub = tm.box_expected(expected_sub, box2) with tm.assert_produces_warning(PerformanceWarning): - res3 = tdi - other + res3 = obj - other tm.assert_equal(res3, expected_sub) @pytest.mark.parametrize("obox", [np.array, pd.Index, pd.Series])