-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Implement integer array add/sub for datetimelike indexes #19959
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
f1147af
39d8ee7
8780466
f4e8e01
70f359d
79e7575
ef6acdb
d477304
df84dc6
e4b6ec8
be737e0
650b1ef
671a008
bcf2419
8cc5270
6cb1b43
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 |
---|---|---|
|
@@ -367,6 +367,49 @@ def test_dti_isub_int(self, tz, one): | |
rng -= one | ||
tm.assert_index_equal(rng, expected) | ||
|
||
# ------------------------------------------------------------- | ||
# __add__/__sub__ with integer arrays | ||
|
||
@pytest.mark.parametrize('freq', ['H', 'D']) | ||
@pytest.mark.parametrize('box', [np.array, pd.Index]) | ||
def test_dti_add_intarray_tick(self, box, freq): | ||
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. we must have some tests that do index arithmetic with integers, either move them here if they are not redundant or remove them. 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. There are tests for integers, but nothing ATM for integer-arrays. |
||
# GH#19959 | ||
dti = pd.date_range('2016-01-01', periods=2, freq=freq) | ||
other = box([4, -1]) | ||
expected = DatetimeIndex([dti[n] + other[n] for n in range(len(dti))]) | ||
result = dti + other | ||
tm.assert_index_equal(result, expected) | ||
result = other + dti | ||
tm.assert_index_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('freq', ['W', 'M', 'MS', 'Q']) | ||
@pytest.mark.parametrize('box', [np.array, pd.Index]) | ||
def test_dti_add_intarray_non_tick(self, box, freq): | ||
# GH#19959 | ||
dti = pd.date_range('2016-01-01', periods=2, freq=freq) | ||
other = box([4, -1]) | ||
expected = DatetimeIndex([dti[n] + other[n] for n in range(len(dti))]) | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
result = dti + other | ||
tm.assert_index_equal(result, expected) | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
result = other + dti | ||
tm.assert_index_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('box', [np.array, pd.Index]) | ||
def test_dti_add_intarray_no_freq(self, box): | ||
# GH#19959 | ||
dti = pd.DatetimeIndex(['2016-01-01', 'NaT', '2017-04-05 06:07:08']) | ||
other = box([9, 4, -1]) | ||
with pytest.raises(NullFrequencyError): | ||
dti + other | ||
with pytest.raises(NullFrequencyError): | ||
other + dti | ||
with pytest.raises(NullFrequencyError): | ||
dti - other | ||
with pytest.raises(TypeError): | ||
other - dti | ||
|
||
# ------------------------------------------------------------- | ||
# DatetimeIndex.shift is used in integer addition | ||
|
||
|
@@ -528,7 +571,7 @@ def test_dti_sub_tdi(self, tz): | |
result = dti - tdi.values | ||
tm.assert_index_equal(result, expected) | ||
|
||
msg = 'cannot perform __neg__ with this index type:' | ||
msg = 'cannot subtract DatetimeIndex from' | ||
with tm.assert_raises_regex(TypeError, msg): | ||
tdi.values - dti | ||
|
||
|
@@ -553,7 +596,8 @@ def test_dti_isub_tdi(self, tz): | |
tm.assert_index_equal(result, expected) | ||
|
||
msg = '|'.join(['cannot perform __neg__ with this index type:', | ||
'ufunc subtract cannot use operands with types']) | ||
'ufunc subtract cannot use operands with types', | ||
'cannot subtract DatetimeIndex from']) | ||
with tm.assert_raises_regex(TypeError, msg): | ||
tdi.values -= dti | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -449,6 +449,31 @@ def test_pi_sub_isub_offset(self): | |
rng -= pd.offsets.MonthEnd(5) | ||
tm.assert_index_equal(rng, expected) | ||
|
||
# --------------------------------------------------------------- | ||
# __add__/__sub__ with integer arrays | ||
|
||
@pytest.mark.parametrize('box', [np.array, pd.Index]) | ||
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. same comment as above |
||
@pytest.mark.parametrize('op', [operator.add, ops.radd]) | ||
def test_pi_add_intarray(self, box, op): | ||
# GH#19959 | ||
pi = pd.PeriodIndex([pd.Period('2015Q1'), pd.Period('NaT')]) | ||
other = box([4, -1]) | ||
result = op(pi, other) | ||
expected = pd.PeriodIndex([pd.Period('2016Q1'), pd.Period('NaT')]) | ||
tm.assert_index_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('box', [np.array, pd.Index]) | ||
def test_pi_sub_intarray(self, box): | ||
# GH#19959 | ||
pi = pd.PeriodIndex([pd.Period('2015Q1'), pd.Period('NaT')]) | ||
other = box([4, -1]) | ||
result = pi - other | ||
expected = pd.PeriodIndex([pd.Period('2014Q1'), pd.Period('NaT')]) | ||
tm.assert_index_equal(result, expected) | ||
|
||
with pytest.raises(TypeError): | ||
other - pi | ||
|
||
# --------------------------------------------------------------- | ||
# Timedelta-like (timedelta, timedelta64, Timedelta, Tick) | ||
# TODO: Some of these are misnomers because of non-Tick DateOffsets | ||
|
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.
can you add a comment on why this is
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.
Addressed, but GitHub UI not surfacing.