Skip to content

Commit

Permalink
Implement remaining components of test matrix following pandas-dev#17991
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Nov 6, 2017
1 parent f7f214b commit 3cb58ac
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 52 deletions.
87 changes: 65 additions & 22 deletions pandas/tests/indexes/datetimes/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@
date_range)


dtinat = pd.to_datetime(['now', 'NaT'])
dtimax = pd.to_datetime(['now', Timestamp.max])
dtimin = pd.to_datetime(['now', Timestamp.min])

tspos = Timestamp('1980-01-01')
ts_pos_variants = [tspos,
tspos.to_pydatetime(),
tspos.to_datetime64().astype('datetime64[ns]'),
tspos.to_datetime64().astype('datetime64[D]')]

tsneg = Timestamp('1950-01-01')
ts_neg_variants = [tsneg,
tsneg.to_pydatetime(),
tsneg.to_datetime64().astype('datetime64[ns]'),
tsneg.to_datetime64().astype('datetime64[D]')]

tdpos = Timedelta('1h')
td_pos_variants = [tdpos,
tdpos.to_pytimedelta(),
tdpos.to_timedelta64().astype('timedelta64[ns]'),
tdpos.to_timedelta64().astype('timedelta64[h]')]

tdneg = Timedelta('-1h')
td_neg_variants = [tdneg,
tdneg.to_pytimedelta(),
tdneg.to_timedelta64().astype('timedelta64[ns]'),
tdneg.to_timedelta64().astype('timedelta64[h]')]


class TestDatetimeIndexArithmetic(object):
tz = [None, 'UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/Asia/Singapore',
'dateutil/US/Pacific']
Expand Down Expand Up @@ -199,35 +228,49 @@ def test_ufunc_coercions(self):
tm.assert_index_equal(result, exp)
assert result.freq == 'D'

def test_datetimeindex_sub_timestamp_overflow(self):
dtimax = pd.to_datetime(['now', pd.Timestamp.max])
dtimin = pd.to_datetime(['now', pd.Timestamp.min])

tsneg = Timestamp('1950-01-01')
ts_neg_variants = [tsneg,
tsneg.to_pydatetime(),
tsneg.to_datetime64().astype('datetime64[ns]'),
tsneg.to_datetime64().astype('datetime64[D]')]
# ------------------------------------------------------------------
# GH17991 checking for overflows and NaT masking on arithmetic ops

def test_dti_add_timedelta_nat_masking(self):
# Checking for NaTs and checking that we don't get an OverflowError
for variant in td_pos_variants + td_neg_variants:
res = dtinat + variant
assert res[1] is NaT

def test_dti_sub_timedelta_nat_masking(self):
# Checking for NaTs and checking that we don't get an OverflowError
for variant in td_pos_variants + td_neg_variants:
res = dtinat - variant
assert res[1] is NaT

def test_dti_sub_timestamp_nat_masking(self):
# Checking for NaTs and checking that we don't get an OverflowError
for variant in ts_pos_variants + ts_neg_variants:
res = dtinat - variant
assert res[1] is NaT

def test_dti_add_timedelta_overflow(self):
for variant in td_pos_variants:
with pytest.raises(OverflowError):
dtimax + variant

tspos = Timestamp('1980-01-01')
ts_pos_variants = [tspos,
tspos.to_pydatetime(),
tspos.to_datetime64().astype('datetime64[ns]'),
tspos.to_datetime64().astype('datetime64[D]')]
for variant in td_neg_variants:
with pytest.raises(OverflowError):
dtimin + variant

for variant in ts_neg_variants:
def test_dti_sub_timedelta_overflow(self):
for variant in td_neg_variants:
with pytest.raises(OverflowError):
dtimax - variant

expected = pd.Timestamp.max.value - tspos.value
for variant in ts_pos_variants:
res = dtimax - variant
assert res[1].value == expected
for variant in td_pos_variants:
with pytest.raises(OverflowError):
dtimin - variant

expected = pd.Timestamp.min.value - tsneg.value
def test_dti_sub_timestamp_overflow(self):
for variant in ts_neg_variants:
res = dtimin - variant
assert res[1].value == expected
with pytest.raises(OverflowError):
dtimax - variant

for variant in ts_pos_variants:
with pytest.raises(OverflowError):
Expand Down
48 changes: 32 additions & 16 deletions pandas/tests/indexes/period/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,47 @@


class TestPeriodIndexArithmetic(object):
def test_add_iadd(self):

# ------------------------------------------------------------------
# PeriodIndex __add__ PeriodIndex operations

# Note: This test also covers __radd__
def test_pi_add_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

def test_pi_add_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
# TODO: Follow-up assertion that rng was not altered in-place?

# Note: This test also covers __rsub__
def test_pi_sub_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

# offset
def test_pi_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
# TODO: Follow-up assertion that rng was not altered in-place?

# ------------------------------------------------------------------

def test_add_iadd(self):
# DateOffset
rng = pd.period_range('2014', '2024', freq='A')
result = rng + pd.offsets.YearEnd(5)
Expand Down Expand Up @@ -121,19 +150,6 @@ def test_sub(self):
tm.assert_index_equal(result, exp)

def test_sub_isub(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

# offset
# DateOffset
rng = pd.period_range('2014', '2024', freq='A')
result = rng - pd.offsets.YearEnd(5)
Expand Down
86 changes: 72 additions & 14 deletions pandas/tests/indexes/timedeltas/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@
Timestamp, Timedelta)


tdinat = pd.to_timedelta(['24658 days 11:15:00', 'NaT'])
tdimax = pd.to_timedelta(['24658 days 11:15:00', Timedelta.max])
tdimin = pd.to_timedelta(['24658 days 11:15:00', Timedelta.min])

tspos = Timestamp('1980-01-01')
ts_pos_variants = [tspos,
tspos.to_pydatetime(),
tspos.to_datetime64().astype('datetime64[ns]'),
tspos.to_datetime64().astype('datetime64[D]')]

tsneg = Timestamp('1950-01-01')
ts_neg_variants = [tsneg,
tsneg.to_pydatetime(),
tsneg.to_datetime64().astype('datetime64[ns]'),
tsneg.to_datetime64().astype('datetime64[D]')]

tdpos = Timedelta('1h')
td_pos_variants = [tdpos,
tdpos.to_pytimedelta(),
tdpos.to_timedelta64().astype('timedelta64[ns]'),
tdpos.to_timedelta64().astype('timedelta64[h]')]

tdneg = Timedelta('-1h')
td_neg_variants = [tdneg,
tdneg.to_pytimedelta(),
tdneg.to_timedelta64().astype('timedelta64[ns]'),
tdneg.to_timedelta64().astype('timedelta64[h]')]


class TestTimedeltaIndexArithmetic(object):
_holder = TimedeltaIndex
_multiprocess_can_split_ = True
Expand Down Expand Up @@ -576,25 +605,54 @@ def test_add_overflow(self):
to_timedelta(['7 seconds', pd.NaT, '4 hours']))
tm.assert_index_equal(result, exp)

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

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

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

def test_tdi_add_timestamp_nat_masking(self):
for variant in ts_neg_variants + ts_pos_variants:
res = tdinat + variant
assert res[1] is pd.NaT
assert res[1] is NaT

def test_tdi_add_timestamp_overflow(self):
for variant in ts_pos_variants:
with pytest.raises(OverflowError):
tdimax + variant

for variant in ts_neg_variants:
with pytest.raises(OverflowError):
tdimin + variant

def test_tdi_add_timedelta_overflow(self):
for variant in td_pos_variants:
with pytest.raises(OverflowError):
tdimax + variant

for variant in td_neg_variants:
with pytest.raises(OverflowError):
tdimin + variant

def test_tdi_sub_timedelta_overflow(self):
for variant in td_neg_variants:
with pytest.raises(OverflowError):
tdimax - variant

for variant in td_pos_variants:
with pytest.raises(OverflowError):
tdimin - variant

# -------------------------------------------------------------

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

0 comments on commit 3cb58ac

Please sign in to comment.