Skip to content
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

TST: parametrize and de-duplicate arithmetic tests #23240

Merged
merged 8 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,32 +323,35 @@ def test_dti_cmp_null_scalar_inequality(self, tz_naive_fixture, other):
with pytest.raises(TypeError):
dti >= other

def test_dti_cmp_nat(self):
@pytest.mark.parametrize('dtype', [None, object])
def test_dti_cmp_nat(self, dtype):
left = pd.DatetimeIndex([pd.Timestamp('2011-01-01'), pd.NaT,
pd.Timestamp('2011-01-03')])
right = pd.DatetimeIndex([pd.NaT, pd.NaT, pd.Timestamp('2011-01-03')])

for lhs, rhs in [(left, right),
(left.astype(object), right.astype(object))]:
result = rhs == lhs
expected = np.array([False, False, True])
tm.assert_numpy_array_equal(result, expected)
lhs, rhs = left, right
if dtype is object:
lhs, rhs = left.astype(object), right.astype(object)

result = rhs == lhs
expected = np.array([False, False, True])
tm.assert_numpy_array_equal(result, expected)

result = lhs != rhs
expected = np.array([True, True, False])
tm.assert_numpy_array_equal(result, expected)
result = lhs != rhs
expected = np.array([True, True, False])
tm.assert_numpy_array_equal(result, expected)

expected = np.array([False, False, False])
tm.assert_numpy_array_equal(lhs == pd.NaT, expected)
tm.assert_numpy_array_equal(pd.NaT == rhs, expected)
expected = np.array([False, False, False])
tm.assert_numpy_array_equal(lhs == pd.NaT, expected)
tm.assert_numpy_array_equal(pd.NaT == rhs, expected)

expected = np.array([True, True, True])
tm.assert_numpy_array_equal(lhs != pd.NaT, expected)
tm.assert_numpy_array_equal(pd.NaT != lhs, expected)
expected = np.array([True, True, True])
tm.assert_numpy_array_equal(lhs != pd.NaT, expected)
tm.assert_numpy_array_equal(pd.NaT != lhs, expected)

expected = np.array([False, False, False])
tm.assert_numpy_array_equal(lhs < pd.NaT, expected)
tm.assert_numpy_array_equal(pd.NaT > lhs, expected)
expected = np.array([False, False, False])
tm.assert_numpy_array_equal(lhs < pd.NaT, expected)
tm.assert_numpy_array_equal(pd.NaT > lhs, expected)

def test_dti_cmp_nat_behaves_like_float_cmp_nan(self):
fidx1 = pd.Index([1.0, np.nan, 3.0, np.nan, 5.0, 7.0])
Expand Down Expand Up @@ -901,13 +904,15 @@ def test_dt64_series_add_intlike(self, tz, op):

other = Series([20, 30, 40], dtype='uint8')

pytest.raises(TypeError, getattr(ser, op), 1)

pytest.raises(TypeError, getattr(ser, op), other)

pytest.raises(TypeError, getattr(ser, op), other.values)

pytest.raises(TypeError, getattr(ser, op), pd.Index(other))
method = getattr(ser, op)
with pytest.raises(TypeError):
method(1)
with pytest.raises(TypeError):
method(other)
with pytest.raises(TypeError):
method(other.values)
with pytest.raises(TypeError):
method(pd.Index(other))

# -------------------------------------------------------------
# Timezone-Centric Tests
Expand Down Expand Up @@ -994,10 +999,6 @@ def test_dti_add_timestamp_raises(self, box):
msg = "cannot add"
with tm.assert_raises_regex(TypeError, msg):
idx + Timestamp('2011-01-01')

def test_dti_radd_timestamp_raises(self):
idx = DatetimeIndex(['2011-01-01', '2011-01-02'])
msg = "cannot add DatetimeIndex and Timestamp"
with tm.assert_raises_regex(TypeError, msg):
Timestamp('2011-01-01') + idx
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided that separating tests for __add__ vs __radd__ was overkill.


Expand Down
162 changes: 59 additions & 103 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,61 +30,80 @@ def test_pi_cmp_period(self):
tm.assert_numpy_array_equal(result, exp)

@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
def test_pi_cmp_pi(self, freq):
def test_parr_cmp_period_scalar(self, freq, box):
# GH#13200
base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
freq=freq)
base = tm.box_expected(base, box)
per = Period('2011-02', freq=freq)

exp = np.array([False, True, False, False])
tm.assert_numpy_array_equal(base == per, exp)
tm.assert_numpy_array_equal(per == base, exp)
tm.assert_equal(base == per, exp)
tm.assert_equal(per == base, exp)

exp = np.array([True, False, True, True])
tm.assert_numpy_array_equal(base != per, exp)
tm.assert_numpy_array_equal(per != base, exp)
tm.assert_equal(base != per, exp)
tm.assert_equal(per != base, exp)

exp = np.array([False, False, True, True])
tm.assert_numpy_array_equal(base > per, exp)
tm.assert_numpy_array_equal(per < base, exp)
tm.assert_equal(base > per, exp)
tm.assert_equal(per < base, exp)

exp = np.array([True, False, False, False])
tm.assert_numpy_array_equal(base < per, exp)
tm.assert_numpy_array_equal(per > base, exp)
tm.assert_equal(base < per, exp)
tm.assert_equal(per > base, exp)

exp = np.array([False, True, True, True])
tm.assert_numpy_array_equal(base >= per, exp)
tm.assert_numpy_array_equal(per <= base, exp)
tm.assert_equal(base >= per, exp)
tm.assert_equal(per <= base, exp)

exp = np.array([True, True, False, False])
tm.assert_numpy_array_equal(base <= per, exp)
tm.assert_numpy_array_equal(per >= base, exp)
tm.assert_equal(base <= per, exp)
tm.assert_equal(per >= base, exp)

@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
def test_parr_cmp_pi(self, freq, box):
# GH#13200
xbox = np.ndarray if box is pd.Index else box

base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
freq=freq)
base = tm.box_expected(base, box)

# TODO: could also box idx?
idx = PeriodIndex(['2011-02', '2011-01', '2011-03', '2011-05'],
freq=freq)

exp = np.array([False, False, True, False])
tm.assert_numpy_array_equal(base == idx, exp)
exp = tm.box_expected(exp, xbox)
tm.assert_equal(base == idx, exp)

exp = np.array([True, True, False, True])
tm.assert_numpy_array_equal(base != idx, exp)
exp = tm.box_expected(exp, xbox)
tm.assert_equal(base != idx, exp)

exp = np.array([False, True, False, False])
tm.assert_numpy_array_equal(base > idx, exp)
exp = tm.box_expected(exp, xbox)
tm.assert_equal(base > idx, exp)

exp = np.array([True, False, False, True])
tm.assert_numpy_array_equal(base < idx, exp)
exp = tm.box_expected(exp, xbox)
tm.assert_equal(base < idx, exp)

exp = np.array([False, True, True, False])
tm.assert_numpy_array_equal(base >= idx, exp)
exp = tm.box_expected(exp, xbox)
tm.assert_equal(base >= idx, exp)

exp = np.array([True, False, True, True])
tm.assert_numpy_array_equal(base <= idx, exp)
exp = tm.box_expected(exp, xbox)
tm.assert_equal(base <= idx, exp)

@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
def test_pi_cmp_pi_mismatched_freq_raises(self, freq):
def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box):
# different base freq
base = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
freq=freq)
base = tm.box_expected(base, box)

msg = "Input has different freq=A-DEC from PeriodIndex"
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
Expand Down Expand Up @@ -197,72 +216,13 @@ def test_comp_nat(self, dtype):


class TestPeriodSeriesComparisons(object):
@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
def test_cmp_series_period_scalar(self, freq):
# GH 13200
base = Series([Period(x, freq=freq) for x in
['2011-01', '2011-02', '2011-03', '2011-04']])
p = Period('2011-02', freq=freq)

exp = Series([False, True, False, False])
tm.assert_series_equal(base == p, exp)
tm.assert_series_equal(p == base, exp)

exp = Series([True, False, True, True])
tm.assert_series_equal(base != p, exp)
tm.assert_series_equal(p != base, exp)

exp = Series([False, False, True, True])
tm.assert_series_equal(base > p, exp)
tm.assert_series_equal(p < base, exp)

exp = Series([True, False, False, False])
tm.assert_series_equal(base < p, exp)
tm.assert_series_equal(p > base, exp)

exp = Series([False, True, True, True])
tm.assert_series_equal(base >= p, exp)
tm.assert_series_equal(p <= base, exp)

exp = Series([True, True, False, False])
tm.assert_series_equal(base <= p, exp)
tm.assert_series_equal(p >= base, exp)

# different base freq
msg = "Input has different freq=A-DEC from Period"
with tm.assert_raises_regex(IncompatibleFrequency, msg):
base <= Period('2011', freq='A')

with tm.assert_raises_regex(IncompatibleFrequency, msg):
Period('2011', freq='A') >= base

@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
def test_cmp_series_period_series(self, freq):
# GH#13200
base = Series([Period(x, freq=freq) for x in
['2011-01', '2011-02', '2011-03', '2011-04']])

ser = Series([Period(x, freq=freq) for x in
['2011-02', '2011-01', '2011-03', '2011-05']])

exp = Series([False, False, True, False])
tm.assert_series_equal(base == ser, exp)

exp = Series([True, True, False, True])
tm.assert_series_equal(base != ser, exp)

exp = Series([False, True, False, False])
tm.assert_series_equal(base > ser, exp)

exp = Series([True, False, False, True])
tm.assert_series_equal(base < ser, exp)

exp = Series([False, True, True, False])
tm.assert_series_equal(base >= ser, exp)

exp = Series([True, False, True, True])
tm.assert_series_equal(base <= ser, exp)

ser2 = Series([Period(x, freq='A') for x in
['2011', '2011', '2011', '2011']])

Expand Down Expand Up @@ -405,9 +365,10 @@ def test_parr_sub_pi_mismatched_freq(self, box_df_broadcast_failure):
@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_pi_add_sub_float(self, op, other):
def test_pi_add_sub_float(self, op, other, box):
dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')
pi = dti.to_period('D')
pi = tm.box_expected(pi, box)
with pytest.raises(TypeError):
op(pi, other)

Expand Down Expand Up @@ -842,11 +803,7 @@ class TestPeriodIndexSeriesMethods(object):
def _check(self, values, func, expected):
idx = pd.PeriodIndex(values)
result = func(idx)
if isinstance(expected, pd.Index):
tm.assert_index_equal(result, expected)
else:
# comp op results in bool
tm.assert_numpy_array_equal(result, expected)
tm.assert_equal(result, expected)

ser = pd.Series(values)
result = func(ser)
Expand Down Expand Up @@ -874,35 +831,34 @@ def test_pi_ops(self):
tm.assert_index_equal(result, exp)

@pytest.mark.parametrize('ng', ["str", 1.5])
def test_pi_ops_errors(self, ng):
def test_pi_ops_errors(self, ng, box):
idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
freq='M', name='idx')
ser = pd.Series(idx)
obj = tm.box_expected(idx, box)

msg = r"unsupported operand type\(s\)"

for obj in [idx, ser]:
with tm.assert_raises_regex(TypeError, msg):
obj + ng
with tm.assert_raises_regex(TypeError, msg):
obj + ng

with pytest.raises(TypeError):
# error message differs between PY2 and 3
ng + obj
with pytest.raises(TypeError):
# error message differs between PY2 and 3
ng + obj

with tm.assert_raises_regex(TypeError, msg):
obj - ng
with tm.assert_raises_regex(TypeError, msg):
obj - ng

with pytest.raises(TypeError):
np.add(obj, ng)
with pytest.raises(TypeError):
np.add(obj, ng)

with pytest.raises(TypeError):
np.add(ng, obj)
with pytest.raises(TypeError):
np.add(ng, obj)

with pytest.raises(TypeError):
np.subtract(obj, ng)
with pytest.raises(TypeError):
np.subtract(obj, ng)

with pytest.raises(TypeError):
np.subtract(ng, obj)
with pytest.raises(TypeError):
np.subtract(ng, obj)

def test_pi_ops_nat(self):
idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'],
Expand Down
Loading