Skip to content

Commit 4eaaf36

Browse files
jbrockmendeljreback
authored andcommitted
TST: parametrize and de-duplicate arithmetic tests (#23240)
1 parent 2b9dfe6 commit 4eaaf36

File tree

4 files changed

+143
-215
lines changed

4 files changed

+143
-215
lines changed

pandas/tests/arithmetic/test_datetime64.py

+30-29
Original file line numberDiff line numberDiff line change
@@ -323,32 +323,35 @@ def test_dti_cmp_null_scalar_inequality(self, tz_naive_fixture, other):
323323
with pytest.raises(TypeError):
324324
dti >= other
325325

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

331-
for lhs, rhs in [(left, right),
332-
(left.astype(object), right.astype(object))]:
333-
result = rhs == lhs
334-
expected = np.array([False, False, True])
335-
tm.assert_numpy_array_equal(result, expected)
332+
lhs, rhs = left, right
333+
if dtype is object:
334+
lhs, rhs = left.astype(object), right.astype(object)
335+
336+
result = rhs == lhs
337+
expected = np.array([False, False, True])
338+
tm.assert_numpy_array_equal(result, expected)
336339

337-
result = lhs != rhs
338-
expected = np.array([True, True, False])
339-
tm.assert_numpy_array_equal(result, expected)
340+
result = lhs != rhs
341+
expected = np.array([True, True, False])
342+
tm.assert_numpy_array_equal(result, expected)
340343

341-
expected = np.array([False, False, False])
342-
tm.assert_numpy_array_equal(lhs == pd.NaT, expected)
343-
tm.assert_numpy_array_equal(pd.NaT == rhs, expected)
344+
expected = np.array([False, False, False])
345+
tm.assert_numpy_array_equal(lhs == pd.NaT, expected)
346+
tm.assert_numpy_array_equal(pd.NaT == rhs, expected)
344347

345-
expected = np.array([True, True, True])
346-
tm.assert_numpy_array_equal(lhs != pd.NaT, expected)
347-
tm.assert_numpy_array_equal(pd.NaT != lhs, expected)
348+
expected = np.array([True, True, True])
349+
tm.assert_numpy_array_equal(lhs != pd.NaT, expected)
350+
tm.assert_numpy_array_equal(pd.NaT != lhs, expected)
348351

349-
expected = np.array([False, False, False])
350-
tm.assert_numpy_array_equal(lhs < pd.NaT, expected)
351-
tm.assert_numpy_array_equal(pd.NaT > lhs, expected)
352+
expected = np.array([False, False, False])
353+
tm.assert_numpy_array_equal(lhs < pd.NaT, expected)
354+
tm.assert_numpy_array_equal(pd.NaT > lhs, expected)
352355

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

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

904-
pytest.raises(TypeError, getattr(ser, op), 1)
905-
906-
pytest.raises(TypeError, getattr(ser, op), other)
907-
908-
pytest.raises(TypeError, getattr(ser, op), other.values)
909-
910-
pytest.raises(TypeError, getattr(ser, op), pd.Index(other))
907+
method = getattr(ser, op)
908+
with pytest.raises(TypeError):
909+
method(1)
910+
with pytest.raises(TypeError):
911+
method(other)
912+
with pytest.raises(TypeError):
913+
method(other.values)
914+
with pytest.raises(TypeError):
915+
method(pd.Index(other))
911916

912917
# -------------------------------------------------------------
913918
# Timezone-Centric Tests
@@ -994,10 +999,6 @@ def test_dti_add_timestamp_raises(self, box):
994999
msg = "cannot add"
9951000
with tm.assert_raises_regex(TypeError, msg):
9961001
idx + Timestamp('2011-01-01')
997-
998-
def test_dti_radd_timestamp_raises(self):
999-
idx = DatetimeIndex(['2011-01-01', '2011-01-02'])
1000-
msg = "cannot add DatetimeIndex and Timestamp"
10011002
with tm.assert_raises_regex(TypeError, msg):
10021003
Timestamp('2011-01-01') + idx
10031004

0 commit comments

Comments
 (0)