1from datetime import timedelta 2 3import numpy as np 4import pytest 5 6from pandas import Interval, Timedelta, Timestamp 7 8 9@pytest.mark.parametrize("method", ["__add__", "__sub__"]) 10@pytest.mark.parametrize( 11 "interval", 12 [ 13 Interval(Timestamp("2017-01-01 00:00:00"), Timestamp("2018-01-01 00:00:00")), 14 Interval(Timedelta(days=7), Timedelta(days=14)), 15 ], 16) 17@pytest.mark.parametrize( 18 "delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")] 19) 20def test_time_interval_add_subtract_timedelta(interval, delta, method): 21 # https://github.com/pandas-dev/pandas/issues/32023 22 result = getattr(interval, method)(delta) 23 left = getattr(interval.left, method)(delta) 24 right = getattr(interval.right, method)(delta) 25 expected = Interval(left, right) 26 27 assert result == expected 28 29 30@pytest.mark.parametrize("interval", [Interval(1, 2), Interval(1.0, 2.0)]) 31@pytest.mark.parametrize( 32 "delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")] 33) 34def test_numeric_interval_add_timedelta_raises(interval, delta): 35 # https://github.com/pandas-dev/pandas/issues/32023 36 msg = "|".join( 37 [ 38 "unsupported operand", 39 "cannot use operands", 40 "Only numeric, Timestamp and Timedelta endpoints are allowed", 41 ] 42 ) 43 with pytest.raises((TypeError, ValueError), match=msg): 44 interval + delta 45 46 with pytest.raises((TypeError, ValueError), match=msg): 47 delta + interval 48 49 50@pytest.mark.parametrize("klass", [timedelta, np.timedelta64, Timedelta]) 51def test_timdelta_add_timestamp_interval(klass): 52 delta = klass(0) 53 expected = Interval(Timestamp("2020-01-01"), Timestamp("2020-02-01")) 54 55 result = delta + expected 56 assert result == expected 57 58 result = expected + delta 59 assert result == expected 60