Skip to content

Commit bc6b7db

Browse files
jbrockmendelproost
authored andcommitted
TST: parametrize and de-duplicate arith tests (pandas-dev#27950)
1 parent 2688345 commit bc6b7db

File tree

5 files changed

+96
-136
lines changed

5 files changed

+96
-136
lines changed

pandas/tests/arithmetic/conftest.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ def box(request):
190190

191191

192192
@pytest.fixture(
193-
params=[pd.Index, pd.Series, pytest.param(pd.DataFrame, marks=pytest.mark.xfail)],
193+
params=[
194+
pd.Index,
195+
pd.Series,
196+
pytest.param(pd.DataFrame, marks=pytest.mark.xfail),
197+
tm.to_array,
198+
],
194199
ids=id_func,
195200
)
196201
def box_df_fail(request):
@@ -206,6 +211,7 @@ def box_df_fail(request):
206211
(pd.Series, False),
207212
(pd.DataFrame, False),
208213
pytest.param((pd.DataFrame, True), marks=pytest.mark.xfail),
214+
(tm.to_array, False),
209215
],
210216
ids=id_func,
211217
)

pandas/tests/arithmetic/test_datetime64.py

+22-42
Original file line numberDiff line numberDiff line change
@@ -348,28 +348,6 @@ def test_dt64arr_timestamp_equality(self, box_with_array):
348348
expected = tm.box_expected([False, False], xbox)
349349
tm.assert_equal(result, expected)
350350

351-
@pytest.mark.parametrize(
352-
"op",
353-
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
354-
)
355-
def test_comparison_tzawareness_compat(self, op):
356-
# GH#18162
357-
dr = pd.date_range("2016-01-01", periods=6)
358-
dz = dr.tz_localize("US/Pacific")
359-
360-
# Check that there isn't a problem aware-aware and naive-naive do not
361-
# raise
362-
naive_series = Series(dr)
363-
aware_series = Series(dz)
364-
msg = "Cannot compare tz-naive and tz-aware"
365-
with pytest.raises(TypeError, match=msg):
366-
op(dz, naive_series)
367-
with pytest.raises(TypeError, match=msg):
368-
op(dr, aware_series)
369-
370-
# TODO: implement _assert_tzawareness_compat for the reverse
371-
# comparison with the Series on the left-hand side
372-
373351

374352
class TestDatetimeIndexComparisons:
375353

@@ -599,15 +577,18 @@ def test_comparison_tzawareness_compat(self, op, box_df_fail):
599577
with pytest.raises(TypeError, match=msg):
600578
op(dz, np.array(list(dr), dtype=object))
601579

602-
# Check that there isn't a problem aware-aware and naive-naive do not
603-
# raise
580+
# The aware==aware and naive==naive comparisons should *not* raise
604581
assert_all(dr == dr)
605-
assert_all(dz == dz)
582+
assert_all(dr == list(dr))
583+
assert_all(list(dr) == dr)
584+
assert_all(np.array(list(dr), dtype=object) == dr)
585+
assert_all(dr == np.array(list(dr), dtype=object))
606586

607-
# FIXME: DataFrame case fails to raise for == and !=, wrong
608-
# message for inequalities
609-
assert (dr == list(dr)).all()
610-
assert (dz == list(dz)).all()
587+
assert_all(dz == dz)
588+
assert_all(dz == list(dz))
589+
assert_all(list(dz) == dz)
590+
assert_all(np.array(list(dz), dtype=object) == dz)
591+
assert_all(dz == np.array(list(dz), dtype=object))
611592

612593
@pytest.mark.parametrize(
613594
"op",
@@ -844,6 +825,7 @@ def test_dt64arr_isub_timedeltalike_scalar(
844825
rng -= two_hours
845826
tm.assert_equal(rng, expected)
846827

828+
# TODO: redundant with test_dt64arr_add_timedeltalike_scalar
847829
def test_dt64arr_add_td64_scalar(self, box_with_array):
848830
# scalar timedeltas/np.timedelta64 objects
849831
# operate with np.timedelta64 correctly
@@ -1709,14 +1691,12 @@ def test_operators_datetimelike(self):
17091691
dt1 - dt2
17101692
dt2 - dt1
17111693

1712-
# ## datetime64 with timetimedelta ###
1694+
# datetime64 with timetimedelta
17131695
dt1 + td1
17141696
td1 + dt1
17151697
dt1 - td1
1716-
# TODO: Decide if this ought to work.
1717-
# td1 - dt1
17181698

1719-
# ## timetimedelta with datetime64 ###
1699+
# timetimedelta with datetime64
17201700
td1 + dt1
17211701
dt1 + td1
17221702

@@ -1914,7 +1894,7 @@ def test_dt64_series_add_intlike(self, tz, op):
19141894
with pytest.raises(TypeError, match=msg):
19151895
method(other)
19161896
with pytest.raises(TypeError, match=msg):
1917-
method(other.values)
1897+
method(np.array(other))
19181898
with pytest.raises(TypeError, match=msg):
19191899
method(pd.Index(other))
19201900

@@ -2380,34 +2360,34 @@ def test_ufunc_coercions(self):
23802360
idx = date_range("2011-01-01", periods=3, freq="2D", name="x")
23812361

23822362
delta = np.timedelta64(1, "D")
2363+
exp = date_range("2011-01-02", periods=3, freq="2D", name="x")
23832364
for result in [idx + delta, np.add(idx, delta)]:
23842365
assert isinstance(result, DatetimeIndex)
2385-
exp = date_range("2011-01-02", periods=3, freq="2D", name="x")
23862366
tm.assert_index_equal(result, exp)
23872367
assert result.freq == "2D"
23882368

2369+
exp = date_range("2010-12-31", periods=3, freq="2D", name="x")
23892370
for result in [idx - delta, np.subtract(idx, delta)]:
23902371
assert isinstance(result, DatetimeIndex)
2391-
exp = date_range("2010-12-31", periods=3, freq="2D", name="x")
23922372
tm.assert_index_equal(result, exp)
23932373
assert result.freq == "2D"
23942374

23952375
delta = np.array(
23962376
[np.timedelta64(1, "D"), np.timedelta64(2, "D"), np.timedelta64(3, "D")]
23972377
)
2378+
exp = DatetimeIndex(
2379+
["2011-01-02", "2011-01-05", "2011-01-08"], freq="3D", name="x"
2380+
)
23982381
for result in [idx + delta, np.add(idx, delta)]:
23992382
assert isinstance(result, DatetimeIndex)
2400-
exp = DatetimeIndex(
2401-
["2011-01-02", "2011-01-05", "2011-01-08"], freq="3D", name="x"
2402-
)
24032383
tm.assert_index_equal(result, exp)
24042384
assert result.freq == "3D"
24052385

2386+
exp = DatetimeIndex(
2387+
["2010-12-31", "2011-01-01", "2011-01-02"], freq="D", name="x"
2388+
)
24062389
for result in [idx - delta, np.subtract(idx, delta)]:
24072390
assert isinstance(result, DatetimeIndex)
2408-
exp = DatetimeIndex(
2409-
["2010-12-31", "2011-01-01", "2011-01-02"], freq="D", name="x"
2410-
)
24112391
tm.assert_index_equal(result, exp)
24122392
assert result.freq == "D"
24132393

pandas/tests/arithmetic/test_numeric.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,9 @@ def test_div_int(self, numeric_idx):
561561
tm.assert_index_equal(result, expected)
562562

563563
@pytest.mark.parametrize("op", [operator.mul, ops.rmul, operator.floordiv])
564-
def test_mul_int_identity(self, op, numeric_idx, box):
564+
def test_mul_int_identity(self, op, numeric_idx, box_with_array):
565565
idx = numeric_idx
566-
idx = tm.box_expected(idx, box)
566+
idx = tm.box_expected(idx, box_with_array)
567567

568568
result = op(idx, 1)
569569
tm.assert_equal(result, idx)
@@ -615,8 +615,9 @@ def test_mul_size_mismatch_raises(self, numeric_idx):
615615
idx * np.array([1, 2])
616616

617617
@pytest.mark.parametrize("op", [operator.pow, ops.rpow])
618-
def test_pow_float(self, op, numeric_idx, box):
618+
def test_pow_float(self, op, numeric_idx, box_with_array):
619619
# test power calculations both ways, GH#14973
620+
box = box_with_array
620621
idx = numeric_idx
621622
expected = pd.Float64Index(op(idx.values, 2.0))
622623

@@ -626,8 +627,9 @@ def test_pow_float(self, op, numeric_idx, box):
626627
result = op(idx, 2.0)
627628
tm.assert_equal(result, expected)
628629

629-
def test_modulo(self, numeric_idx, box):
630+
def test_modulo(self, numeric_idx, box_with_array):
630631
# GH#9244
632+
box = box_with_array
631633
idx = numeric_idx
632634
expected = Index(idx.values % 2)
633635

@@ -1041,7 +1043,8 @@ class TestObjectDtypeEquivalence:
10411043
# Tests that arithmetic operations match operations executed elementwise
10421044

10431045
@pytest.mark.parametrize("dtype", [None, object])
1044-
def test_numarr_with_dtype_add_nan(self, dtype, box):
1046+
def test_numarr_with_dtype_add_nan(self, dtype, box_with_array):
1047+
box = box_with_array
10451048
ser = pd.Series([1, 2, 3], dtype=dtype)
10461049
expected = pd.Series([np.nan, np.nan, np.nan], dtype=dtype)
10471050

@@ -1055,7 +1058,8 @@ def test_numarr_with_dtype_add_nan(self, dtype, box):
10551058
tm.assert_equal(result, expected)
10561059

10571060
@pytest.mark.parametrize("dtype", [None, object])
1058-
def test_numarr_with_dtype_add_int(self, dtype, box):
1061+
def test_numarr_with_dtype_add_int(self, dtype, box_with_array):
1062+
box = box_with_array
10591063
ser = pd.Series([1, 2, 3], dtype=dtype)
10601064
expected = pd.Series([2, 3, 4], dtype=dtype)
10611065

pandas/tests/arithmetic/test_object.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ def test_pow_ops_object(self):
8989

9090
@pytest.mark.parametrize("op", [operator.add, ops.radd])
9191
@pytest.mark.parametrize("other", ["category", "Int64"])
92-
def test_add_extension_scalar(self, other, box, op):
92+
def test_add_extension_scalar(self, other, box_with_array, op):
9393
# GH#22378
9494
# Check that scalars satisfying is_extension_array_dtype(obj)
9595
# do not incorrectly try to dispatch to an ExtensionArray operation
9696

9797
arr = pd.Series(["a", "b", "c"])
9898
expected = pd.Series([op(x, other) for x in arr])
9999

100-
arr = tm.box_expected(arr, box)
101-
expected = tm.box_expected(expected, box)
100+
arr = tm.box_expected(arr, box_with_array)
101+
expected = tm.box_expected(expected, box_with_array)
102102

103103
result = op(arr, other)
104104
tm.assert_equal(result, expected)
@@ -133,16 +133,17 @@ def test_objarr_radd_str(self, box):
133133
],
134134
)
135135
@pytest.mark.parametrize("dtype", [None, object])
136-
def test_objarr_radd_str_invalid(self, dtype, data, box):
136+
def test_objarr_radd_str_invalid(self, dtype, data, box_with_array):
137137
ser = Series(data, dtype=dtype)
138138

139-
ser = tm.box_expected(ser, box)
139+
ser = tm.box_expected(ser, box_with_array)
140140
with pytest.raises(TypeError):
141141
"foo_" + ser
142142

143143
@pytest.mark.parametrize("op", [operator.add, ops.radd, operator.sub, ops.rsub])
144-
def test_objarr_add_invalid(self, op, box):
144+
def test_objarr_add_invalid(self, op, box_with_array):
145145
# invalid ops
146+
box = box_with_array
146147

147148
obj_ser = tm.makeObjectSeries()
148149
obj_ser.name = "objects"

0 commit comments

Comments
 (0)