Skip to content

Commit 3cb58ac

Browse files
committed
Implement remaining components of test matrix following pandas-dev#17991
1 parent f7f214b commit 3cb58ac

File tree

3 files changed

+169
-52
lines changed

3 files changed

+169
-52
lines changed

pandas/tests/indexes/datetimes/test_arithmetic.py

+65-22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,35 @@
1414
date_range)
1515

1616

17+
dtinat = pd.to_datetime(['now', 'NaT'])
18+
dtimax = pd.to_datetime(['now', Timestamp.max])
19+
dtimin = pd.to_datetime(['now', Timestamp.min])
20+
21+
tspos = Timestamp('1980-01-01')
22+
ts_pos_variants = [tspos,
23+
tspos.to_pydatetime(),
24+
tspos.to_datetime64().astype('datetime64[ns]'),
25+
tspos.to_datetime64().astype('datetime64[D]')]
26+
27+
tsneg = Timestamp('1950-01-01')
28+
ts_neg_variants = [tsneg,
29+
tsneg.to_pydatetime(),
30+
tsneg.to_datetime64().astype('datetime64[ns]'),
31+
tsneg.to_datetime64().astype('datetime64[D]')]
32+
33+
tdpos = Timedelta('1h')
34+
td_pos_variants = [tdpos,
35+
tdpos.to_pytimedelta(),
36+
tdpos.to_timedelta64().astype('timedelta64[ns]'),
37+
tdpos.to_timedelta64().astype('timedelta64[h]')]
38+
39+
tdneg = Timedelta('-1h')
40+
td_neg_variants = [tdneg,
41+
tdneg.to_pytimedelta(),
42+
tdneg.to_timedelta64().astype('timedelta64[ns]'),
43+
tdneg.to_timedelta64().astype('timedelta64[h]')]
44+
45+
1746
class TestDatetimeIndexArithmetic(object):
1847
tz = [None, 'UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/Asia/Singapore',
1948
'dateutil/US/Pacific']
@@ -199,35 +228,49 @@ def test_ufunc_coercions(self):
199228
tm.assert_index_equal(result, exp)
200229
assert result.freq == 'D'
201230

202-
def test_datetimeindex_sub_timestamp_overflow(self):
203-
dtimax = pd.to_datetime(['now', pd.Timestamp.max])
204-
dtimin = pd.to_datetime(['now', pd.Timestamp.min])
205-
206-
tsneg = Timestamp('1950-01-01')
207-
ts_neg_variants = [tsneg,
208-
tsneg.to_pydatetime(),
209-
tsneg.to_datetime64().astype('datetime64[ns]'),
210-
tsneg.to_datetime64().astype('datetime64[D]')]
231+
# ------------------------------------------------------------------
232+
# GH17991 checking for overflows and NaT masking on arithmetic ops
233+
234+
def test_dti_add_timedelta_nat_masking(self):
235+
# Checking for NaTs and checking that we don't get an OverflowError
236+
for variant in td_pos_variants + td_neg_variants:
237+
res = dtinat + variant
238+
assert res[1] is NaT
239+
240+
def test_dti_sub_timedelta_nat_masking(self):
241+
# Checking for NaTs and checking that we don't get an OverflowError
242+
for variant in td_pos_variants + td_neg_variants:
243+
res = dtinat - variant
244+
assert res[1] is NaT
245+
246+
def test_dti_sub_timestamp_nat_masking(self):
247+
# Checking for NaTs and checking that we don't get an OverflowError
248+
for variant in ts_pos_variants + ts_neg_variants:
249+
res = dtinat - variant
250+
assert res[1] is NaT
251+
252+
def test_dti_add_timedelta_overflow(self):
253+
for variant in td_pos_variants:
254+
with pytest.raises(OverflowError):
255+
dtimax + variant
211256

212-
tspos = Timestamp('1980-01-01')
213-
ts_pos_variants = [tspos,
214-
tspos.to_pydatetime(),
215-
tspos.to_datetime64().astype('datetime64[ns]'),
216-
tspos.to_datetime64().astype('datetime64[D]')]
257+
for variant in td_neg_variants:
258+
with pytest.raises(OverflowError):
259+
dtimin + variant
217260

218-
for variant in ts_neg_variants:
261+
def test_dti_sub_timedelta_overflow(self):
262+
for variant in td_neg_variants:
219263
with pytest.raises(OverflowError):
220264
dtimax - variant
221265

222-
expected = pd.Timestamp.max.value - tspos.value
223-
for variant in ts_pos_variants:
224-
res = dtimax - variant
225-
assert res[1].value == expected
266+
for variant in td_pos_variants:
267+
with pytest.raises(OverflowError):
268+
dtimin - variant
226269

227-
expected = pd.Timestamp.min.value - tsneg.value
270+
def test_dti_sub_timestamp_overflow(self):
228271
for variant in ts_neg_variants:
229-
res = dtimin - variant
230-
assert res[1].value == expected
272+
with pytest.raises(OverflowError):
273+
dtimax - variant
231274

232275
for variant in ts_pos_variants:
233276
with pytest.raises(OverflowError):

pandas/tests/indexes/period/test_arithmetic.py

+32-16
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,47 @@
1212

1313

1414
class TestPeriodIndexArithmetic(object):
15-
def test_add_iadd(self):
15+
16+
# ------------------------------------------------------------------
17+
# PeriodIndex __add__ PeriodIndex operations
18+
19+
# Note: This test also covers __radd__
20+
def test_pi_add_pi_raises(self):
1621
rng = pd.period_range('1/1/2000', freq='D', periods=5)
1722
other = pd.period_range('1/6/2000', freq='D', periods=5)
18-
1923
# previously performed setop union, now raises TypeError (GH14164)
2024
with pytest.raises(TypeError):
2125
rng + other
2226

27+
def test_pi_add_pi_raises(self):
28+
rng = pd.period_range('1/1/2000', freq='D', periods=5)
29+
other = pd.period_range('1/6/2000', freq='D', periods=5)
30+
# previously performed setop union, now raises TypeError (GH14164)
2331
with pytest.raises(TypeError):
2432
rng += other
33+
# TODO: Follow-up assertion that rng was not altered in-place?
34+
35+
# Note: This test also covers __rsub__
36+
def test_pi_sub_pi_raises(self):
37+
# previously performed setop, now raises TypeError (GH14164)
38+
# TODO needs to wait on #13077 for decision on result type
39+
rng = pd.period_range('1/1/2000', freq='D', periods=5)
40+
other = pd.period_range('1/6/2000', freq='D', periods=5)
41+
with pytest.raises(TypeError):
42+
rng - other
2543

26-
# offset
44+
def test_pi_isub_pi_raises(self):
45+
# previously performed setop, now raises TypeError (GH14164)
46+
# TODO needs to wait on #13077 for decision on result type
47+
rng = pd.period_range('1/1/2000', freq='D', periods=5)
48+
other = pd.period_range('1/6/2000', freq='D', periods=5)
49+
with pytest.raises(TypeError):
50+
rng -= other
51+
# TODO: Follow-up assertion that rng was not altered in-place?
52+
53+
# ------------------------------------------------------------------
54+
55+
def test_add_iadd(self):
2756
# DateOffset
2857
rng = pd.period_range('2014', '2024', freq='A')
2958
result = rng + pd.offsets.YearEnd(5)
@@ -121,19 +150,6 @@ def test_sub(self):
121150
tm.assert_index_equal(result, exp)
122151

123152
def test_sub_isub(self):
124-
125-
# previously performed setop, now raises TypeError (GH14164)
126-
# TODO needs to wait on #13077 for decision on result type
127-
rng = pd.period_range('1/1/2000', freq='D', periods=5)
128-
other = pd.period_range('1/6/2000', freq='D', periods=5)
129-
130-
with pytest.raises(TypeError):
131-
rng - other
132-
133-
with pytest.raises(TypeError):
134-
rng -= other
135-
136-
# offset
137153
# DateOffset
138154
rng = pd.period_range('2014', '2024', freq='A')
139155
result = rng - pd.offsets.YearEnd(5)

pandas/tests/indexes/timedeltas/test_arithmetic.py

+72-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,35 @@
1212
Timestamp, Timedelta)
1313

1414

15+
tdinat = pd.to_timedelta(['24658 days 11:15:00', 'NaT'])
16+
tdimax = pd.to_timedelta(['24658 days 11:15:00', Timedelta.max])
17+
tdimin = pd.to_timedelta(['24658 days 11:15:00', Timedelta.min])
18+
19+
tspos = Timestamp('1980-01-01')
20+
ts_pos_variants = [tspos,
21+
tspos.to_pydatetime(),
22+
tspos.to_datetime64().astype('datetime64[ns]'),
23+
tspos.to_datetime64().astype('datetime64[D]')]
24+
25+
tsneg = Timestamp('1950-01-01')
26+
ts_neg_variants = [tsneg,
27+
tsneg.to_pydatetime(),
28+
tsneg.to_datetime64().astype('datetime64[ns]'),
29+
tsneg.to_datetime64().astype('datetime64[D]')]
30+
31+
tdpos = Timedelta('1h')
32+
td_pos_variants = [tdpos,
33+
tdpos.to_pytimedelta(),
34+
tdpos.to_timedelta64().astype('timedelta64[ns]'),
35+
tdpos.to_timedelta64().astype('timedelta64[h]')]
36+
37+
tdneg = Timedelta('-1h')
38+
td_neg_variants = [tdneg,
39+
tdneg.to_pytimedelta(),
40+
tdneg.to_timedelta64().astype('timedelta64[ns]'),
41+
tdneg.to_timedelta64().astype('timedelta64[h]')]
42+
43+
1544
class TestTimedeltaIndexArithmetic(object):
1645
_holder = TimedeltaIndex
1746
_multiprocess_can_split_ = True
@@ -576,25 +605,54 @@ def test_add_overflow(self):
576605
to_timedelta(['7 seconds', pd.NaT, '4 hours']))
577606
tm.assert_index_equal(result, exp)
578607

579-
def test_timedeltaindex_add_timestamp_nat_masking(self):
580-
# GH17991 checking for overflow-masking with NaT
581-
tdinat = pd.to_timedelta(['24658 days 11:15:00', 'NaT'])
608+
# -------------------------------------------------------------
609+
# GH17991 checking for overflows and NaT masking on arithmetic ops
582610

583-
tsneg = Timestamp('1950-01-01')
584-
ts_neg_variants = [tsneg,
585-
tsneg.to_pydatetime(),
586-
tsneg.to_datetime64().astype('datetime64[ns]'),
587-
tsneg.to_datetime64().astype('datetime64[D]')]
611+
def test_tdi_add_timedelta_nat_masking(self):
612+
# Checking for NaTs and checking that we don't get an OverflowError
613+
for variant in td_pos_variants + td_neg_variants:
614+
res = tdinat + variant
615+
assert res[1] is NaT
588616

589-
tspos = Timestamp('1980-01-01')
590-
ts_pos_variants = [tspos,
591-
tspos.to_pydatetime(),
592-
tspos.to_datetime64().astype('datetime64[ns]'),
593-
tspos.to_datetime64().astype('datetime64[D]')]
617+
def test_tdi_sub_timedelta_nat_masking(self):
618+
# Checking for NaTs and checking that we don't get an OverflowError
619+
for variant in td_pos_variants + td_neg_variants:
620+
res = tdinat - variant
621+
assert res[1] is NaT
594622

623+
def test_tdi_add_timestamp_nat_masking(self):
595624
for variant in ts_neg_variants + ts_pos_variants:
596625
res = tdinat + variant
597-
assert res[1] is pd.NaT
626+
assert res[1] is NaT
627+
628+
def test_tdi_add_timestamp_overflow(self):
629+
for variant in ts_pos_variants:
630+
with pytest.raises(OverflowError):
631+
tdimax + variant
632+
633+
for variant in ts_neg_variants:
634+
with pytest.raises(OverflowError):
635+
tdimin + variant
636+
637+
def test_tdi_add_timedelta_overflow(self):
638+
for variant in td_pos_variants:
639+
with pytest.raises(OverflowError):
640+
tdimax + variant
641+
642+
for variant in td_neg_variants:
643+
with pytest.raises(OverflowError):
644+
tdimin + variant
645+
646+
def test_tdi_sub_timedelta_overflow(self):
647+
for variant in td_neg_variants:
648+
with pytest.raises(OverflowError):
649+
tdimax - variant
650+
651+
for variant in td_pos_variants:
652+
with pytest.raises(OverflowError):
653+
tdimin - variant
654+
655+
# -------------------------------------------------------------
598656

599657
def test_tdi_ops_attributes(self):
600658
rng = timedelta_range('2 days', periods=5, freq='2D', name='x')

0 commit comments

Comments
 (0)