Skip to content

Commit f3958f0

Browse files
authored
TST: fixturize, collect (#44242)
1 parent eb00c22 commit f3958f0

29 files changed

+193
-217
lines changed

pandas/conftest.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -995,17 +995,19 @@ def all_reductions(request):
995995
return request.param
996996

997997

998-
@pytest.fixture(params=["__eq__", "__ne__", "__le__", "__lt__", "__ge__", "__gt__"])
999-
def all_compare_operators(request):
998+
@pytest.fixture(
999+
params=[
1000+
operator.eq,
1001+
operator.ne,
1002+
operator.gt,
1003+
operator.ge,
1004+
operator.lt,
1005+
operator.le,
1006+
]
1007+
)
1008+
def comparison_op(request):
10001009
"""
1001-
Fixture for dunder names for common compare operations
1002-
1003-
* >=
1004-
* >
1005-
* ==
1006-
* !=
1007-
* <
1008-
* <=
1010+
Fixture for operator module comparison functions.
10091011
"""
10101012
return request.param
10111013

pandas/tests/arithmetic/test_datetime64.py

+13-27
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,14 @@ def test_dt64arr_timestamp_equality(self, box_with_array):
365365
class TestDatetimeIndexComparisons:
366366

367367
# TODO: moved from tests.indexes.test_base; parametrize and de-duplicate
368-
@pytest.mark.parametrize(
369-
"op",
370-
[operator.eq, operator.ne, operator.gt, operator.lt, operator.ge, operator.le],
371-
)
372-
def test_comparators(self, op):
368+
def test_comparators(self, comparison_op):
373369
index = tm.makeDateIndex(100)
374370
element = index[len(index) // 2]
375371
element = Timestamp(element).to_datetime64()
376372

377373
arr = np.array(index)
378-
arr_result = op(arr, element)
379-
index_result = op(index, element)
374+
arr_result = comparison_op(arr, element)
375+
index_result = comparison_op(index, element)
380376

381377
assert isinstance(index_result, np.ndarray)
382378
tm.assert_numpy_array_equal(arr_result, index_result)
@@ -554,12 +550,9 @@ def test_dti_cmp_nat_behaves_like_float_cmp_nan(self):
554550
expected = np.array([True, True, False, True, True, True])
555551
tm.assert_numpy_array_equal(result, expected)
556552

557-
@pytest.mark.parametrize(
558-
"op",
559-
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
560-
)
561-
def test_comparison_tzawareness_compat(self, op, box_with_array):
553+
def test_comparison_tzawareness_compat(self, comparison_op, box_with_array):
562554
# GH#18162
555+
op = comparison_op
563556
box = box_with_array
564557

565558
dr = date_range("2016-01-01", periods=6)
@@ -606,12 +599,10 @@ def test_comparison_tzawareness_compat(self, op, box_with_array):
606599
assert np.all(np.array(tolist(dz), dtype=object) == dz)
607600
assert np.all(dz == np.array(tolist(dz), dtype=object))
608601

609-
@pytest.mark.parametrize(
610-
"op",
611-
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
612-
)
613-
def test_comparison_tzawareness_compat_scalars(self, op, box_with_array):
602+
def test_comparison_tzawareness_compat_scalars(self, comparison_op, box_with_array):
614603
# GH#18162
604+
op = comparison_op
605+
615606
dr = date_range("2016-01-01", periods=6)
616607
dz = dr.tz_localize("US/Pacific")
617608

@@ -638,10 +629,6 @@ def test_comparison_tzawareness_compat_scalars(self, op, box_with_array):
638629
with pytest.raises(TypeError, match=msg):
639630
op(ts, dz)
640631

641-
@pytest.mark.parametrize(
642-
"op",
643-
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
644-
)
645632
@pytest.mark.parametrize(
646633
"other",
647634
[datetime(2016, 1, 1), Timestamp("2016-01-01"), np.datetime64("2016-01-01")],
@@ -652,8 +639,9 @@ def test_comparison_tzawareness_compat_scalars(self, op, box_with_array):
652639
@pytest.mark.filterwarnings("ignore:elementwise comp:DeprecationWarning")
653640
@pytest.mark.filterwarnings("ignore:Converting timezone-aware:FutureWarning")
654641
def test_scalar_comparison_tzawareness(
655-
self, op, other, tz_aware_fixture, box_with_array
642+
self, comparison_op, other, tz_aware_fixture, box_with_array
656643
):
644+
op = comparison_op
657645
box = box_with_array
658646
tz = tz_aware_fixture
659647
dti = date_range("2016-01-01", periods=2, tz=tz)
@@ -680,13 +668,11 @@ def test_scalar_comparison_tzawareness(
680668
with pytest.raises(TypeError, match=msg):
681669
op(other, dtarr)
682670

683-
@pytest.mark.parametrize(
684-
"op",
685-
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
686-
)
687-
def test_nat_comparison_tzawareness(self, op):
671+
def test_nat_comparison_tzawareness(self, comparison_op):
688672
# GH#19276
689673
# tzaware DatetimeIndex should not raise when compared to NaT
674+
op = comparison_op
675+
690676
dti = DatetimeIndex(
691677
["2014-01-01", NaT, "2014-03-01", NaT, "2014-05-01", "2014-07-01"]
692678
)

pandas/tests/arrays/boolean/test_comparison.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,23 @@ def dtype():
2121

2222

2323
class TestComparisonOps(ComparisonOps):
24-
def test_compare_scalar(self, data, all_compare_operators):
25-
op_name = all_compare_operators
26-
self._compare_other(data, op_name, True)
24+
def test_compare_scalar(self, data, comparison_op):
25+
self._compare_other(data, comparison_op, True)
2726

28-
def test_compare_array(self, data, all_compare_operators):
29-
op_name = all_compare_operators
27+
def test_compare_array(self, data, comparison_op):
3028
other = pd.array([True] * len(data), dtype="boolean")
31-
self._compare_other(data, op_name, other)
29+
self._compare_other(data, comparison_op, other)
3230
other = np.array([True] * len(data))
33-
self._compare_other(data, op_name, other)
31+
self._compare_other(data, comparison_op, other)
3432
other = pd.Series([True] * len(data))
35-
self._compare_other(data, op_name, other)
33+
self._compare_other(data, comparison_op, other)
3634

3735
@pytest.mark.parametrize("other", [True, False, pd.NA])
38-
def test_scalar(self, other, all_compare_operators, dtype):
39-
ComparisonOps.test_scalar(self, other, all_compare_operators, dtype)
36+
def test_scalar(self, other, comparison_op, dtype):
37+
ComparisonOps.test_scalar(self, other, comparison_op, dtype)
4038

41-
def test_array(self, all_compare_operators):
42-
op = self.get_op_from_name(all_compare_operators)
39+
def test_array(self, comparison_op):
40+
op = comparison_op
4341
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
4442
b = pd.array([True, False, None] * 3, dtype="boolean")
4543

pandas/tests/arrays/categorical/test_operators.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import operator
21
import warnings
32

43
import numpy as np
@@ -145,9 +144,9 @@ def test_compare_frame(self):
145144
expected = DataFrame([[False, True, True, False]])
146145
tm.assert_frame_equal(result, expected)
147146

148-
def test_compare_frame_raises(self, all_compare_operators):
147+
def test_compare_frame_raises(self, comparison_op):
149148
# alignment raises unless we transpose
150-
op = getattr(operator, all_compare_operators)
149+
op = comparison_op
151150
cat = Categorical(["a", "b", 2, "a"])
152151
df = DataFrame(cat)
153152
msg = "Unable to coerce to Series, length must be 1: given 4"

pandas/tests/arrays/floating/test_comparison.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
class TestComparisonOps(NumericOps, ComparisonOps):
1212
@pytest.mark.parametrize("other", [True, False, pd.NA, -1.0, 0.0, 1])
13-
def test_scalar(self, other, all_compare_operators, dtype):
14-
ComparisonOps.test_scalar(self, other, all_compare_operators, dtype)
13+
def test_scalar(self, other, comparison_op, dtype):
14+
ComparisonOps.test_scalar(self, other, comparison_op, dtype)
1515

16-
def test_compare_with_integerarray(self, all_compare_operators):
17-
op = self.get_op_from_name(all_compare_operators)
16+
def test_compare_with_integerarray(self, comparison_op):
17+
op = comparison_op
1818
a = pd.array([0, 1, None] * 3, dtype="Int64")
1919
b = pd.array([0] * 3 + [1] * 3 + [None] * 3, dtype="Float64")
2020
other = b.astype("Int64")

pandas/tests/arrays/integer/test_comparison.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99

1010
class TestComparisonOps(NumericOps, ComparisonOps):
1111
@pytest.mark.parametrize("other", [True, False, pd.NA, -1, 0, 1])
12-
def test_scalar(self, other, all_compare_operators, dtype):
13-
ComparisonOps.test_scalar(self, other, all_compare_operators, dtype)
12+
def test_scalar(self, other, comparison_op, dtype):
13+
ComparisonOps.test_scalar(self, other, comparison_op, dtype)
1414

15-
def test_compare_to_int(self, dtype, all_compare_operators):
15+
def test_compare_to_int(self, dtype, comparison_op):
1616
# GH 28930
17+
op_name = f"__{comparison_op.__name__}__"
1718
s1 = pd.Series([1, None, 3], dtype=dtype)
1819
s2 = pd.Series([1, None, 3], dtype="float")
1920

20-
method = getattr(s1, all_compare_operators)
21+
method = getattr(s1, op_name)
2122
result = method(2)
2223

23-
method = getattr(s2, all_compare_operators)
24+
method = getattr(s2, op_name)
2425
expected = method(2).astype("boolean")
2526
expected[s2.isna()] = pd.NA
2627

pandas/tests/arrays/masked_shared.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010

1111
class ComparisonOps(BaseOpsUtil):
12-
def _compare_other(self, data, op_name, other):
13-
op = self.get_op_from_name(op_name)
12+
def _compare_other(self, data, op, other):
1413

1514
# array
1615
result = pd.Series(op(data, other))
@@ -34,8 +33,8 @@ def _compare_other(self, data, op_name, other):
3433
tm.assert_series_equal(result, expected)
3534

3635
# subclass will override to parametrize 'other'
37-
def test_scalar(self, other, all_compare_operators, dtype):
38-
op = self.get_op_from_name(all_compare_operators)
36+
def test_scalar(self, other, comparison_op, dtype):
37+
op = comparison_op
3938
left = pd.array([1, 0, None], dtype=dtype)
4039

4140
result = op(left, other)
@@ -59,8 +58,8 @@ def test_no_shared_mask(self, data):
5958
result = data + 1
6059
assert np.shares_memory(result._mask, data._mask) is False
6160

62-
def test_array(self, all_compare_operators, dtype):
63-
op = self.get_op_from_name(all_compare_operators)
61+
def test_array(self, comparison_op, dtype):
62+
op = comparison_op
6463

6564
left = pd.array([0, 1, 2, None, None, None], dtype=dtype)
6665
right = pd.array([0, 1, None, 0, 1, None], dtype=dtype)
@@ -81,8 +80,8 @@ def test_array(self, all_compare_operators, dtype):
8180
right, pd.array([0, 1, None, 0, 1, None], dtype=dtype)
8281
)
8382

84-
def test_compare_with_booleanarray(self, all_compare_operators, dtype):
85-
op = self.get_op_from_name(all_compare_operators)
83+
def test_compare_with_booleanarray(self, comparison_op, dtype):
84+
op = comparison_op
8685

8786
left = pd.array([True, False, None] * 3, dtype="boolean")
8887
right = pd.array([0] * 3 + [1] * 3 + [None] * 3, dtype=dtype)

pandas/tests/arrays/string_/test_string.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ def test_add_frame(dtype):
199199
tm.assert_frame_equal(result, expected)
200200

201201

202-
def test_comparison_methods_scalar(all_compare_operators, dtype):
203-
op_name = all_compare_operators
202+
def test_comparison_methods_scalar(comparison_op, dtype):
203+
op_name = f"__{comparison_op.__name__}__"
204204
a = pd.array(["a", None, "c"], dtype=dtype)
205205
other = "a"
206206
result = getattr(a, op_name)(other)
@@ -209,21 +209,21 @@ def test_comparison_methods_scalar(all_compare_operators, dtype):
209209
tm.assert_extension_array_equal(result, expected)
210210

211211

212-
def test_comparison_methods_scalar_pd_na(all_compare_operators, dtype):
213-
op_name = all_compare_operators
212+
def test_comparison_methods_scalar_pd_na(comparison_op, dtype):
213+
op_name = f"__{comparison_op.__name__}__"
214214
a = pd.array(["a", None, "c"], dtype=dtype)
215215
result = getattr(a, op_name)(pd.NA)
216216
expected = pd.array([None, None, None], dtype="boolean")
217217
tm.assert_extension_array_equal(result, expected)
218218

219219

220-
def test_comparison_methods_scalar_not_string(all_compare_operators, dtype, request):
221-
if all_compare_operators not in ["__eq__", "__ne__"]:
220+
def test_comparison_methods_scalar_not_string(comparison_op, dtype, request):
221+
op_name = f"__{comparison_op.__name__}__"
222+
if op_name not in ["__eq__", "__ne__"]:
222223
reason = "comparison op not supported between instances of 'str' and 'int'"
223224
mark = pytest.mark.xfail(raises=TypeError, reason=reason)
224225
request.node.add_marker(mark)
225226

226-
op_name = all_compare_operators
227227
a = pd.array(["a", None, "c"], dtype=dtype)
228228
other = 42
229229
result = getattr(a, op_name)(other)
@@ -234,14 +234,14 @@ def test_comparison_methods_scalar_not_string(all_compare_operators, dtype, requ
234234
tm.assert_extension_array_equal(result, expected)
235235

236236

237-
def test_comparison_methods_array(all_compare_operators, dtype, request):
237+
def test_comparison_methods_array(comparison_op, dtype, request):
238238
if dtype.storage == "pyarrow":
239239
mark = pytest.mark.xfail(
240240
raises=AssertionError, reason="left is not an ExtensionArray"
241241
)
242242
request.node.add_marker(mark)
243243

244-
op_name = all_compare_operators
244+
op_name = f"__{comparison_op.__name__}__"
245245

246246
a = pd.array(["a", None, "c"], dtype=dtype)
247247
other = [None, None, "c"]

pandas/tests/arrays/test_datetimes.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""
22
Tests for DatetimeArray
33
"""
4-
import operator
5-
64
import numpy as np
75
import pytest
86

@@ -17,10 +15,9 @@ class TestDatetimeArrayComparisons:
1715
# TODO: merge this into tests/arithmetic/test_datetime64 once it is
1816
# sufficiently robust
1917

20-
def test_cmp_dt64_arraylike_tznaive(self, all_compare_operators):
18+
def test_cmp_dt64_arraylike_tznaive(self, comparison_op):
2119
# arbitrary tz-naive DatetimeIndex
22-
opname = all_compare_operators.strip("_")
23-
op = getattr(operator, opname)
20+
op = comparison_op
2421

2522
dti = pd.date_range("2016-01-1", freq="MS", periods=9, tz=None)
2623
arr = DatetimeArray(dti)
@@ -30,7 +27,7 @@ def test_cmp_dt64_arraylike_tznaive(self, all_compare_operators):
3027
right = dti
3128

3229
expected = np.ones(len(arr), dtype=bool)
33-
if opname in ["ne", "gt", "lt"]:
30+
if comparison_op.__name__ in ["ne", "gt", "lt"]:
3431
# for these the comparisons should be all-False
3532
expected = ~expected
3633

pandas/tests/extension/base/getitem.py

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def test_getitem_invalid(self, data):
138138
"list index out of range", # json
139139
"index out of bounds", # pyarrow
140140
"Out of bounds access", # Sparse
141+
f"loc must be an integer between -{ub} and {ub}", # Sparse
141142
f"index {ub+1} is out of bounds for axis 0 with size {ub}",
142143
f"index -{ub+1} is out of bounds for axis 0 with size {ub}",
143144
]

pandas/tests/extension/base/ops.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,9 @@ def test_direct_arith_with_ndframe_returns_not_implemented(self, data, box):
130130
class BaseComparisonOpsTests(BaseOpsUtil):
131131
"""Various Series and DataFrame comparison ops methods."""
132132

133-
def _compare_other(self, ser: pd.Series, data, op_name: str, other):
133+
def _compare_other(self, ser: pd.Series, data, op, other):
134134

135-
op = self.get_op_from_name(op_name)
136-
if op_name in ["__eq__", "__ne__"]:
135+
if op.__name__ in ["eq", "ne"]:
137136
# comparison should match point-wise comparisons
138137
result = op(ser, other)
139138
expected = ser.combine(other, op)
@@ -154,23 +153,22 @@ def _compare_other(self, ser: pd.Series, data, op_name: str, other):
154153
with pytest.raises(type(exc)):
155154
ser.combine(other, op)
156155

157-
def test_compare_scalar(self, data, all_compare_operators):
158-
op_name = all_compare_operators
156+
def test_compare_scalar(self, data, comparison_op):
159157
ser = pd.Series(data)
160-
self._compare_other(ser, data, op_name, 0)
158+
self._compare_other(ser, data, comparison_op, 0)
161159

162-
def test_compare_array(self, data, all_compare_operators):
163-
op_name = all_compare_operators
160+
def test_compare_array(self, data, comparison_op):
164161
ser = pd.Series(data)
165162
other = pd.Series([data[0]] * len(data))
166-
self._compare_other(ser, data, op_name, other)
163+
self._compare_other(ser, data, comparison_op, other)
167164

168-
@pytest.mark.parametrize("box", [pd.Series, pd.DataFrame])
169-
def test_direct_arith_with_ndframe_returns_not_implemented(self, data, box):
165+
def test_direct_arith_with_ndframe_returns_not_implemented(
166+
self, data, frame_or_series
167+
):
170168
# EAs should return NotImplemented for ops with Series/DataFrame
171169
# Pandas takes care of unboxing the series and calling the EA's op.
172170
other = pd.Series(data)
173-
if box is pd.DataFrame:
171+
if frame_or_series is pd.DataFrame:
174172
other = other.to_frame()
175173

176174
if hasattr(data, "__eq__"):

0 commit comments

Comments
 (0)