-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix TimedeltaIndex +/- offset array #19095
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 1 commit
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
to_timedelta, timedelta_range, date_range, | ||
Series, | ||
Timestamp, Timedelta) | ||
from pandas.errors import PerformanceWarning | ||
|
||
|
||
@pytest.fixture(params=[pd.offsets.Hour(2), timedelta(hours=2), | ||
|
@@ -28,23 +29,98 @@ def freq(request): | |
class TestTimedeltaIndexArithmetic(object): | ||
_holder = TimedeltaIndex | ||
|
||
@pytest.mark.xfail(reason='GH#18824 ufunc add cannot use operands...') | ||
def test_tdi_with_offset_array(self): | ||
@pytest.mark.parametrize('box', [np.array, pd.Index]) | ||
def test_tdi_add_offset_array(self, box): | ||
# GH#18849 | ||
tdi = pd.TimedeltaIndex(['1 days 00:00:00', '3 days 04:00:00']) | ||
offs = np.array([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)]) | ||
expected = pd.TimedeltaIndex(['1 days 01:00:00', '3 days 04:02:00']) | ||
tdi = TimedeltaIndex(['1 days 00:00:00', '3 days 04:00:00']) | ||
other = np.array([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)]) | ||
|
||
res = tdi + offs | ||
expected = TimedeltaIndex([tdi[n] + other[n] for n in range(len(tdi))], | ||
name=tdi.name, freq='infer') | ||
|
||
with tm.assert_produces_warning(PerformanceWarning): | ||
res = tdi + other | ||
tm.assert_index_equal(res, expected) | ||
|
||
res2 = offs + tdi | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
res2 = other + tdi | ||
tm.assert_index_equal(res2, expected) | ||
|
||
anchored = np.array([pd.offsets.QuarterEnd(), | ||
pd.offsets.Week(weekday=2)]) | ||
anchored = box([pd.offsets.QuarterEnd(), | ||
pd.offsets.Week(weekday=2)]) | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
tdi + anchored | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
anchored + tdi | ||
|
||
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. ideally could parameterize some of these for index, ndarray & Series as well (you can leave them here is ok). I believe you have some duplication in test_base 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. Yah, part of it is tricky because parametrizing with |
||
@pytest.mark.parametrize('box', [np.array, pd.Index]) | ||
def test_tdi_sub_offset_array(self, box): | ||
# GH#18824 | ||
tdi = TimedeltaIndex(['1 days 00:00:00', '3 days 04:00:00']) | ||
other = np.array([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)]) | ||
|
||
expected = TimedeltaIndex([tdi[n] - other[n] for n in range(len(tdi))], | ||
name=tdi.name, freq='infer') | ||
|
||
with tm.assert_produces_warning(PerformanceWarning): | ||
res = tdi - other | ||
tm.assert_index_equal(res, expected) | ||
|
||
anchored = box([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]) | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
tdi - anchored | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
anchored - tdi | ||
|
||
@pytest.mark.parametrize('names', [(None, None, None), | ||
('foo', 'bar', None), | ||
('foo', 'foo', 'foo')]) | ||
def test_tdi_with_offset_series(self, names): | ||
# GH#18849 | ||
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=names[2]) | ||
|
||
with tm.assert_produces_warning(PerformanceWarning): | ||
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. I would put a comment on before these sub-section of tests, hard to follow your flow others, IOW enumerate the cases. only looking for a comment at lines 89, 100, 107 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. Will do. |
||
res = tdi + other | ||
tm.assert_series_equal(res, expected_add) | ||
|
||
with tm.assert_produces_warning(PerformanceWarning): | ||
res2 = other + tdi | ||
tm.assert_series_equal(res2, expected_add) | ||
|
||
expected_sub = Series([tdi[n] - other[n] for n in range(len(tdi))], | ||
name=names[2]) | ||
|
||
with tm.assert_produces_warning(PerformanceWarning): | ||
res3 = tdi - other | ||
tm.assert_series_equal(res3, expected_sub) | ||
|
||
anchored = Series([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)], | ||
name=names[1]) | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
anchored + tdi | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
tdi + anchored | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
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. some duplication here |
||
anchored + tdi | ||
with pytest.raises(TypeError): | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
tdi - anchored | ||
with pytest.raises(TypeError): | ||
tdi + anchored | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
anchored - tdi | ||
|
||
# TODO: Split by ops, better name | ||
def test_numeric_compat(self): | ||
|
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.
shouldn't you be using the box here?
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.
Good catch.