Skip to content

TST: series offset artithmetic tests #34582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
84 changes: 83 additions & 1 deletion pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ class TestDatetime64Arithmetic:
# over DataFrame/Series/Index/DatetimeArray

# -------------------------------------------------------------
# Addition/Subtraction of timedelta-like
# Addition/Subtraction of timedelta-like scalars

def test_dt64arr_add_timedeltalike_scalar(
self, tz_naive_fixture, two_hours, box_with_array
Expand Down Expand Up @@ -778,6 +778,48 @@ def test_dt64arr_iadd_timedeltalike_scalar(
rng += two_hours
tm.assert_equal(rng, expected)

@pytest.mark.filterwarnings(
"ignore:Adding/subtracting object-dtype array to DatetimeArray"
)
@pytest.mark.parametrize(
"dt64arr, offset_arr, expected",
[
(
[
pd.Timestamp("2000-01-01"),
pd.Timestamp("2000-02-01"),
pd.Timestamp("2000-05-01"),
],
[
pd.offsets.DateOffset(years=1),
pd.offsets.DateOffset(months=2),
pd.offsets.MonthEnd(),
],
[
pd.Timestamp("2001-01-01"),
pd.Timestamp("2000-04-01"),
pd.Timestamp("2000-05-31"),
],
)
],
)
@pytest.mark.parametrize(
"constr_dt, constr_offset, constr_expected",
[
(pd.Series, pd.Series, pd.Series),
(pd.Series, pd.Index, pd.Series),
(pd.DatetimeIndex, pd.Series, pd.Series),
],
)
def test_dt64arr_add_offset_scalar(
self, dt64arr, offset_arr, expected, constr_dt, constr_offset, constr_expected,
):
# GH#19211
result1 = constr_offset(offset_arr) + constr_dt(dt64arr)
result2 = constr_dt(dt64arr) + constr_offset(offset_arr)
tm.assert_series_equal(result1, constr_expected(expected))
tm.assert_series_equal(result2, constr_expected(expected))

def test_dt64arr_sub_timedeltalike_scalar(
self, tz_naive_fixture, two_hours, box_with_array
):
Expand Down Expand Up @@ -806,6 +848,46 @@ def test_dt64arr_isub_timedeltalike_scalar(
rng -= two_hours
tm.assert_equal(rng, expected)

@pytest.mark.filterwarnings(
"ignore:Adding/subtracting object-dtype array to DatetimeArray"
)
@pytest.mark.parametrize(
"dt64arr, offset_arr, expected",
[
(
[
pd.Timestamp("2000-01-01"),
pd.Timestamp("2000-03-29"),
pd.Timestamp("2000-05-15"),
],
[
pd.offsets.DateOffset(years=1),
pd.offsets.DateOffset(months=2),
pd.offsets.MonthBegin(),
],
[
pd.Timestamp("1999-1-1"),
pd.Timestamp("2000-1-29"),
pd.Timestamp("2000-05-01"),
],
)
],
)
@pytest.mark.parametrize(
"constr_dt, constr_offset, constr_expected",
[
(pd.Series, pd.Series, pd.Series),
(pd.Series, pd.Index, pd.Series),
(pd.DatetimeIndex, pd.Series, pd.Series),
],
)
def test_dt64arr_sub_offset_scalar(
self, dt64arr, offset_arr, expected, constr_dt, constr_offset, constr_expected,
):
# GH#19211
result = constr_dt(dt64arr) - constr_offset(offset_arr)
tm.assert_series_equal(result, constr_expected(expected))

# TODO: redundant with test_dt64arr_add_timedeltalike_scalar
def test_dt64arr_add_td64_scalar(self, box_with_array):
# scalar timedeltas/np.timedelta64 objects
Expand Down