Skip to content

Commit

Permalink
Centralize m8[ns] Arithmetic Tests (#22118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jul 31, 2018
1 parent c272c52 commit 7c67d9c
Show file tree
Hide file tree
Showing 3 changed files with 743 additions and 431 deletions.
72 changes: 0 additions & 72 deletions pandas/tests/indexes/timedeltas/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import operator

import pytest
import numpy as np
Expand All @@ -13,7 +12,6 @@
Series,
Timestamp, Timedelta)
from pandas.errors import PerformanceWarning, NullFrequencyError
from pandas.core import ops


@pytest.fixture(params=[pd.offsets.Hour(2), timedelta(hours=2),
Expand Down Expand Up @@ -270,53 +268,6 @@ def test_tdi_floordiv_timedelta_scalar(self, scalar_td):
class TestTimedeltaIndexArithmetic(object):
# Addition and Subtraction Operations

# -------------------------------------------------------------
# Invalid Operations

@pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])])
@pytest.mark.parametrize('op', [operator.add, ops.radd,
operator.sub, ops.rsub])
def test_tdi_add_sub_float(self, op, other):
dti = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
tdi = dti - dti.shift(1)
with pytest.raises(TypeError):
op(tdi, other)

def test_tdi_add_str_invalid(self):
# GH 13624
tdi = TimedeltaIndex(['1 day', '2 days'])

with pytest.raises(TypeError):
tdi + 'a'
with pytest.raises(TypeError):
'a' + tdi

@pytest.mark.parametrize('freq', [None, 'H'])
def test_tdi_sub_period(self, freq):
# GH#13078
# not supported, check TypeError
p = pd.Period('2011-01-01', freq='D')

idx = pd.TimedeltaIndex(['1 hours', '2 hours'], freq=freq)

with pytest.raises(TypeError):
idx - p

with pytest.raises(TypeError):
p - idx

@pytest.mark.parametrize('op', [operator.add, ops.radd,
operator.sub, ops.rsub])
@pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H'])
@pytest.mark.parametrize('tdi_freq', [None, 'H'])
def test_dti_sub_pi(self, tdi_freq, pi_freq, op):
# GH#20049 subtracting PeriodIndex should raise TypeError
tdi = pd.TimedeltaIndex(['1 hours', '2 hours'], freq=tdi_freq)
dti = pd.Timestamp('2018-03-07 17:16:40') + tdi
pi = dti.to_period(pi_freq)
with pytest.raises(TypeError):
op(dti, pi)

# -------------------------------------------------------------
# TimedeltaIndex.shift is used by __add__/__sub__

Expand Down Expand Up @@ -626,29 +577,6 @@ def test_tdi_isub_timedeltalike(self, delta):
rng -= delta
tm.assert_index_equal(rng, expected)

# -------------------------------------------------------------
# Binary operations TimedeltaIndex and datetime-like

def test_tdi_sub_timestamp_raises(self):
idx = TimedeltaIndex(['1 day', '2 day'])
msg = "cannot subtract a datelike from a TimedeltaIndex"
with tm.assert_raises_regex(TypeError, msg):
idx - Timestamp('2011-01-01')

def test_tdi_add_timestamp(self):
idx = TimedeltaIndex(['1 day', '2 day'])

result = idx + Timestamp('2011-01-01')
expected = DatetimeIndex(['2011-01-02', '2011-01-03'])
tm.assert_index_equal(result, expected)

def test_tdi_radd_timestamp(self):
idx = TimedeltaIndex(['1 day', '2 day'])

result = Timestamp('2011-01-01') + idx
expected = DatetimeIndex(['2011-01-02', '2011-01-03'])
tm.assert_index_equal(result, expected)

# -------------------------------------------------------------
# __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64]

Expand Down
Loading

0 comments on commit 7c67d9c

Please sign in to comment.