-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
95f2fbe
7438110
4e2c07a
5b30740
d88aca0
48e4794
4fdc04f
ceb54d1
fe10a03
b5f169f
6cc226d
d052240
a46a0ba
784706f
63c7825
51c3c66
fa93176
9ff29a9
ab97990
4955456
e11023a
985f94e
ec57620
0c3a0ed
746f542
3d2f913
6e49272
1bc6304
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from datetime import timedelta | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from pandas import Interval, Timedelta, Timestamp | ||
|
||
|
||
@pytest.mark.parametrize("method", ["__add__", "__sub__"]) | ||
@pytest.mark.parametrize( | ||
"interval", | ||
[ | ||
Interval(Timestamp("2017-01-01 00:00:00"), Timestamp("2018-01-01 00:00:00")), | ||
Interval(Timedelta(days=7), Timedelta(days=14)), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")] | ||
) | ||
def test_time_interval_add_subtract_timedelta(interval, delta, method): | ||
# https://github.com/pandas-dev/pandas/issues/32023 | ||
result = getattr(interval, method)(delta) | ||
left = getattr(interval.left, method)(delta) | ||
right = getattr(interval.right, method)(delta) | ||
expected = Interval(left, right) | ||
|
||
assert result == expected | ||
|
||
|
||
@pytest.mark.parametrize("interval", [Interval(1, 2), Interval(1.0, 2.0)]) | ||
@pytest.mark.parametrize( | ||
"delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")] | ||
) | ||
def test_numeric_interval_add_timedelta_raises(interval, delta): | ||
# https://github.com/pandas-dev/pandas/issues/32023 | ||
msg = "|".join( | ||
[ | ||
"unsupported operand", | ||
"cannot use operands", | ||
"Only numeric, Timestamp and Timedelta endpoints are allowed", | ||
] | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we get a Timedelta interval here too? also some NaTs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to put NaT in an Interval? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intervals can't contain NA values as endpoints. It doesn't look like this gracefully handles adding In [1]: import numpy as np; import pandas as pd; pd.__version__
Out[1]: '1.1.0.dev0+1299.g6e492722d'
In [2]: pd.Interval(0, 1) + np.nan
---------------------------------------------------------------------------
ValueError: left side of interval must be <= right side
In [3]: pd.Interval(pd.Timestamp("2020"), pd.Timestamp("2021")) + pd.NaT
---------------------------------------------------------------------------
TypeError: unsupported operand type(s) for +: 'pandas._libs.interval.Interval' and 'NaTType' |
||
with pytest.raises((TypeError, ValueError), match=msg): | ||
interval + delta | ||
|
||
with pytest.raises((TypeError, ValueError), match=msg): | ||
delta + interval |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some additional
Interval
related arithmetic tests that can be moved here fromtest_interval.py
but doesn't need to be done in this PR.