-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: PeriodIndex subtraction to return object Index of DateOffsets #20049
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
7563876
14f8d86
a623c5a
24ebba2
f2dac59
945ed39
0adf604
eb6ec2a
e609631
e3cfc26
59b314f
0f6d67d
82810d3
6637a4f
0886917
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 |
---|---|---|
|
@@ -258,6 +258,57 @@ def test_comp_nat(self, dtype): | |
|
||
|
||
class TestPeriodIndexArithmetic(object): | ||
# --------------------------------------------------------------- | ||
# __add__/__sub__ with PeriodIndex | ||
# PeriodIndex + other is defined for integers and timedelta-like others | ||
# PeriodIndex - other is defined for integers, timedelta-like others, | ||
# and PeriodIndex (with matching freq) | ||
|
||
def test_pi_add_iadd_pi_raises(self): | ||
rng = pd.period_range('1/1/2000', freq='D', periods=5) | ||
other = pd.period_range('1/6/2000', freq='D', periods=5) | ||
|
||
# An earlier implementation of PeriodIndex addition performed | ||
# a set operation (union). This has since been changed to | ||
# raise a TypeError. See GH#14164 and GH#13077 for historical | ||
# reference. | ||
with pytest.raises(TypeError): | ||
rng + other | ||
|
||
with pytest.raises(TypeError): | ||
rng += other | ||
|
||
def test_pi_sub_isub_pi(self): | ||
# GH#20049 | ||
# For historical reference see GH#14164, GH#13077. | ||
# PeriodIndex subtraction originally performed set difference, | ||
# then changed to raise TypeError before being implemented in GH#20049 | ||
rng = pd.period_range('1/1/2000', freq='D', periods=5) | ||
other = pd.period_range('1/6/2000', freq='D', periods=5) | ||
|
||
off = rng.freq | ||
expected = pd.Index([-5 * off] * 5) | ||
result = rng - other | ||
tm.assert_index_equal(result, expected) | ||
|
||
rng -= other | ||
tm.assert_index_equal(rng, expected) | ||
|
||
def test_pi_sub_pi_with_nat(self): | ||
rng = pd.period_range('1/1/2000', freq='D', periods=5) | ||
other = rng[1:].insert(0, pd.NaT) | ||
assert other[1:].equals(rng[1:]) | ||
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 this 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. This is checking that I haven't already screwed up, i.e. that the test below is indeed testing what I think it is testing. |
||
|
||
result = rng - other | ||
off = rng.freq | ||
expected = pd.Index([pd.NaT, 0 * off, 0 * off, 0 * off, 0 * off]) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_pi_sub_pi_mismatched_freq(self): | ||
rng = pd.period_range('1/1/2000', freq='D', periods=5) | ||
other = pd.period_range('1/6/2000', freq='H', periods=5) | ||
with pytest.raises(period.IncompatibleFrequency): | ||
rng - other | ||
|
||
# ------------------------------------------------------------- | ||
# Invalid Operations | ||
|
@@ -379,17 +430,6 @@ def test_pi_sub_offset_array(self, box): | |
with tm.assert_produces_warning(PerformanceWarning): | ||
anchored - pi | ||
|
||
def test_pi_add_iadd_pi_raises(self): | ||
rng = pd.period_range('1/1/2000', freq='D', periods=5) | ||
other = pd.period_range('1/6/2000', freq='D', periods=5) | ||
|
||
# previously performed setop union, now raises TypeError (GH14164) | ||
with pytest.raises(TypeError): | ||
rng + other | ||
|
||
with pytest.raises(TypeError): | ||
rng += other | ||
|
||
def test_pi_add_iadd_int(self, one): | ||
# Variants of `one` for #19012 | ||
rng = pd.period_range('2000-01-01 09:00', freq='H', periods=10) | ||
|
@@ -419,18 +459,6 @@ def test_pi_sub_intlike(self, five): | |
exp = rng + (-five) | ||
tm.assert_index_equal(result, exp) | ||
|
||
def test_pi_sub_isub_pi_raises(self): | ||
# previously performed setop, now raises TypeError (GH14164) | ||
# TODO needs to wait on #13077 for decision on result type | ||
rng = pd.period_range('1/1/2000', freq='D', periods=5) | ||
other = pd.period_range('1/6/2000', freq='D', periods=5) | ||
|
||
with pytest.raises(TypeError): | ||
rng - other | ||
|
||
with pytest.raises(TypeError): | ||
rng -= other | ||
|
||
def test_pi_sub_isub_offset(self): | ||
# offset | ||
# DateOffset | ||
|
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 (maybe another test) add/sub with a pi and another dtype (I don't think we have this test?)
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.
Just checking, you mean to add cases over in the period test_arithmetic file, not here, right?