Skip to content

BUG: Allow addition of Timedelta to Timestamp interval #32107

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

Merged
merged 28 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
95f2fbe
Add test
dsaxton Feb 19, 2020
7438110
Check for Timedelta
dsaxton Feb 19, 2020
4e2c07a
Update test
dsaxton Feb 19, 2020
5b30740
whatsnew
dsaxton Feb 19, 2020
d88aca0
Remove unreachable
dsaxton Feb 19, 2020
48e4794
Revert "Remove unreachable"
dsaxton Feb 19, 2020
4fdc04f
Merge branch 'master' into add-timedelta
dsaxton Feb 19, 2020
ceb54d1
Merge branch 'master' into add-timedelta
dsaxton Feb 20, 2020
fe10a03
Update tests
dsaxton Feb 21, 2020
b5f169f
Tests
dsaxton Feb 21, 2020
6cc226d
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Feb 21, 2020
d052240
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Feb 25, 2020
a46a0ba
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 2, 2020
784706f
Check some other types
dsaxton Apr 2, 2020
63c7825
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 5, 2020
51c3c66
Maybe fix something
dsaxton Apr 10, 2020
fa93176
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 10, 2020
9ff29a9
Remove np.timdelta64 test case for now
dsaxton Apr 11, 2020
ab97990
Lint
dsaxton Apr 11, 2020
4955456
Black
dsaxton Apr 11, 2020
e11023a
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 11, 2020
985f94e
timedelta64 -> np.timedelta64
dsaxton Apr 11, 2020
ec57620
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 11, 2020
0c3a0ed
__array_priority__
dsaxton Apr 11, 2020
746f542
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 14, 2020
3d2f913
Update
dsaxton Apr 15, 2020
6e49272
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 15, 2020
1bc6304
Merge remote-tracking branch 'upstream/master' into add-timedelta
dsaxton Apr 24, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Strings
Interval
^^^^^^^^

-
- Fixed bug in :class:`Interval` where a :class:`Timedelta` could not be added or subtracted from a :class:`Timestamp` interval (:issue:`32023`)
-

Indexing
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,14 @@ cdef class Interval(IntervalMixin):
return f'{start_symbol}{left}, {right}{end_symbol}'

def __add__(self, y):
if isinstance(y, numbers.Number):
if isinstance(y, (numbers.Number, Timedelta)):
return Interval(self.left + y, self.right + y, closed=self.closed)
elif isinstance(y, Interval) and isinstance(self, numbers.Number):
return Interval(y.left + self, y.right + self, closed=y.closed)
return NotImplemented

def __sub__(self, y):
if isinstance(y, numbers.Number):
if isinstance(y, (numbers.Number, Timedelta)):
return Interval(self.left - y, self.right - y, closed=self.closed)
return NotImplemented

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/arithmetic/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,18 @@ def test_index_series_compat(self, op, constructor, expected_type, assert_func):
result = op(index, other)
expected = expected_type(self.elementwise_comparison(op, index, other))
assert_func(result, expected)


@pytest.mark.parametrize("method", ["__add__", "__sub__"])
def test_timestamp_interval_add_subtract_timedelta(method):
# https://github.com/pandas-dev/pandas/issues/32023
interval = Interval(
Timestamp("2017-01-01 00:00:00"), Timestamp("2018-01-01 00:00:00")
)
delta = Timedelta(days=7)
result = getattr(interval, method)(delta)
left = getattr(interval.left, method)(delta)
right = getattr(interval.right, method)(delta)
expected = Interval(left, right)

assert result == expected