Skip to content

TST: fixturize, collect #44242

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

Merged
merged 6 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 12 additions & 10 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,17 +995,19 @@ def all_reductions(request):
return request.param


@pytest.fixture(params=["__eq__", "__ne__", "__le__", "__lt__", "__ge__", "__gt__"])
def all_compare_operators(request):
@pytest.fixture(
params=[
operator.eq,
operator.ne,
operator.gt,
operator.ge,
operator.lt,
operator.le,
]
)
def comparison_op(request):
"""
Fixture for dunder names for common compare operations

* >=
* >
* ==
* !=
* <
* <=
Fixture for operator module comparison functions.
"""
return request.param

Expand Down
40 changes: 13 additions & 27 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,14 @@ def test_dt64arr_timestamp_equality(self, box_with_array):
class TestDatetimeIndexComparisons:

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

arr = np.array(index)
arr_result = op(arr, element)
index_result = op(index, element)
arr_result = comparison_op(arr, element)
index_result = comparison_op(index, element)

assert isinstance(index_result, np.ndarray)
tm.assert_numpy_array_equal(arr_result, index_result)
Expand Down Expand Up @@ -554,12 +550,9 @@ def test_dti_cmp_nat_behaves_like_float_cmp_nan(self):
expected = np.array([True, True, False, True, True, True])
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize(
"op",
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
)
def test_comparison_tzawareness_compat(self, op, box_with_array):
def test_comparison_tzawareness_compat(self, comparison_op, box_with_array):
# GH#18162
op = comparison_op
box = box_with_array

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

@pytest.mark.parametrize(
"op",
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
)
def test_comparison_tzawareness_compat_scalars(self, op, box_with_array):
def test_comparison_tzawareness_compat_scalars(self, comparison_op, box_with_array):
# GH#18162
op = comparison_op

dr = date_range("2016-01-01", periods=6)
dz = dr.tz_localize("US/Pacific")

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

@pytest.mark.parametrize(
"op",
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
)
@pytest.mark.parametrize(
"other",
[datetime(2016, 1, 1), Timestamp("2016-01-01"), np.datetime64("2016-01-01")],
Expand All @@ -652,8 +639,9 @@ def test_comparison_tzawareness_compat_scalars(self, op, box_with_array):
@pytest.mark.filterwarnings("ignore:elementwise comp:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:Converting timezone-aware:FutureWarning")
def test_scalar_comparison_tzawareness(
self, op, other, tz_aware_fixture, box_with_array
self, comparison_op, other, tz_aware_fixture, box_with_array
):
op = comparison_op
box = box_with_array
tz = tz_aware_fixture
dti = date_range("2016-01-01", periods=2, tz=tz)
Expand All @@ -680,13 +668,11 @@ def test_scalar_comparison_tzawareness(
with pytest.raises(TypeError, match=msg):
op(other, dtarr)

@pytest.mark.parametrize(
"op",
[operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le],
)
def test_nat_comparison_tzawareness(self, op):
def test_nat_comparison_tzawareness(self, comparison_op):
# GH#19276
# tzaware DatetimeIndex should not raise when compared to NaT
op = comparison_op

dti = DatetimeIndex(
["2014-01-01", NaT, "2014-03-01", NaT, "2014-05-01", "2014-07-01"]
)
Expand Down
22 changes: 10 additions & 12 deletions pandas/tests/arrays/boolean/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,23 @@ def dtype():


class TestComparisonOps(ComparisonOps):
def test_compare_scalar(self, data, all_compare_operators):
op_name = all_compare_operators
self._compare_other(data, op_name, True)
def test_compare_scalar(self, data, comparison_op):
self._compare_other(data, comparison_op, True)

def test_compare_array(self, data, all_compare_operators):
op_name = all_compare_operators
def test_compare_array(self, data, comparison_op):
other = pd.array([True] * len(data), dtype="boolean")
self._compare_other(data, op_name, other)
self._compare_other(data, comparison_op, other)
other = np.array([True] * len(data))
self._compare_other(data, op_name, other)
self._compare_other(data, comparison_op, other)
other = pd.Series([True] * len(data))
self._compare_other(data, op_name, other)
self._compare_other(data, comparison_op, other)

@pytest.mark.parametrize("other", [True, False, pd.NA])
def test_scalar(self, other, all_compare_operators, dtype):
ComparisonOps.test_scalar(self, other, all_compare_operators, dtype)
def test_scalar(self, other, comparison_op, dtype):
ComparisonOps.test_scalar(self, other, comparison_op, dtype)

def test_array(self, all_compare_operators):
op = self.get_op_from_name(all_compare_operators)
def test_array(self, comparison_op):
op = comparison_op
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
b = pd.array([True, False, None] * 3, dtype="boolean")

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import operator
import warnings

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

def test_compare_frame_raises(self, all_compare_operators):
def test_compare_frame_raises(self, comparison_op):
# alignment raises unless we transpose
op = getattr(operator, all_compare_operators)
op = comparison_op
cat = Categorical(["a", "b", 2, "a"])
df = DataFrame(cat)
msg = "Unable to coerce to Series, length must be 1: given 4"
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/arrays/floating/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

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

def test_compare_with_integerarray(self, all_compare_operators):
op = self.get_op_from_name(all_compare_operators)
def test_compare_with_integerarray(self, comparison_op):
op = comparison_op
a = pd.array([0, 1, None] * 3, dtype="Int64")
b = pd.array([0] * 3 + [1] * 3 + [None] * 3, dtype="Float64")
other = b.astype("Int64")
Expand Down
11 changes: 6 additions & 5 deletions pandas/tests/arrays/integer/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@

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

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

method = getattr(s1, all_compare_operators)
method = getattr(s1, op_name)
result = method(2)

method = getattr(s2, all_compare_operators)
method = getattr(s2, op_name)
expected = method(2).astype("boolean")
expected[s2.isna()] = pd.NA

Expand Down
15 changes: 7 additions & 8 deletions pandas/tests/arrays/masked_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@


class ComparisonOps(BaseOpsUtil):
def _compare_other(self, data, op_name, other):
op = self.get_op_from_name(op_name)
def _compare_other(self, data, op, other):

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

# subclass will override to parametrize 'other'
def test_scalar(self, other, all_compare_operators, dtype):
op = self.get_op_from_name(all_compare_operators)
def test_scalar(self, other, comparison_op, dtype):
op = comparison_op
left = pd.array([1, 0, None], dtype=dtype)

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

def test_array(self, all_compare_operators, dtype):
op = self.get_op_from_name(all_compare_operators)
def test_array(self, comparison_op, dtype):
op = comparison_op

left = pd.array([0, 1, 2, None, None, None], dtype=dtype)
right = pd.array([0, 1, None, 0, 1, None], dtype=dtype)
Expand All @@ -81,8 +80,8 @@ def test_array(self, all_compare_operators, dtype):
right, pd.array([0, 1, None, 0, 1, None], dtype=dtype)
)

def test_compare_with_booleanarray(self, all_compare_operators, dtype):
op = self.get_op_from_name(all_compare_operators)
def test_compare_with_booleanarray(self, comparison_op, dtype):
op = comparison_op

left = pd.array([True, False, None] * 3, dtype="boolean")
right = pd.array([0] * 3 + [1] * 3 + [None] * 3, dtype=dtype)
Expand Down
18 changes: 9 additions & 9 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ def test_add_frame(dtype):
tm.assert_frame_equal(result, expected)


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


def test_comparison_methods_scalar_pd_na(all_compare_operators, dtype):
op_name = all_compare_operators
def test_comparison_methods_scalar_pd_na(comparison_op, dtype):
op_name = f"__{comparison_op.__name__}__"
a = pd.array(["a", None, "c"], dtype=dtype)
result = getattr(a, op_name)(pd.NA)
expected = pd.array([None, None, None], dtype="boolean")
tm.assert_extension_array_equal(result, expected)


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

op_name = all_compare_operators
a = pd.array(["a", None, "c"], dtype=dtype)
other = 42
result = getattr(a, op_name)(other)
Expand All @@ -234,14 +234,14 @@ def test_comparison_methods_scalar_not_string(all_compare_operators, dtype, requ
tm.assert_extension_array_equal(result, expected)


def test_comparison_methods_array(all_compare_operators, dtype, request):
def test_comparison_methods_array(comparison_op, dtype, request):
if dtype.storage == "pyarrow":
mark = pytest.mark.xfail(
raises=AssertionError, reason="left is not an ExtensionArray"
)
request.node.add_marker(mark)

op_name = all_compare_operators
op_name = f"__{comparison_op.__name__}__"

a = pd.array(["a", None, "c"], dtype=dtype)
other = [None, None, "c"]
Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Tests for DatetimeArray
"""
import operator

import numpy as np
import pytest

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

def test_cmp_dt64_arraylike_tznaive(self, all_compare_operators):
def test_cmp_dt64_arraylike_tznaive(self, comparison_op):
# arbitrary tz-naive DatetimeIndex
opname = all_compare_operators.strip("_")
op = getattr(operator, opname)
op = comparison_op

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

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

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def test_getitem_invalid(self, data):
"list index out of range", # json
"index out of bounds", # pyarrow
"Out of bounds access", # Sparse
f"loc must be an integer between -{ub} and {ub}", # Sparse
f"index {ub+1} is out of bounds for axis 0 with size {ub}",
f"index -{ub+1} is out of bounds for axis 0 with size {ub}",
]
Expand Down
22 changes: 10 additions & 12 deletions pandas/tests/extension/base/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ def test_direct_arith_with_ndframe_returns_not_implemented(self, data, box):
class BaseComparisonOpsTests(BaseOpsUtil):
"""Various Series and DataFrame comparison ops methods."""

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

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

def test_compare_scalar(self, data, all_compare_operators):
op_name = all_compare_operators
def test_compare_scalar(self, data, comparison_op):
ser = pd.Series(data)
self._compare_other(ser, data, op_name, 0)
self._compare_other(ser, data, comparison_op, 0)

def test_compare_array(self, data, all_compare_operators):
op_name = all_compare_operators
def test_compare_array(self, data, comparison_op):
ser = pd.Series(data)
other = pd.Series([data[0]] * len(data))
self._compare_other(ser, data, op_name, other)
self._compare_other(ser, data, comparison_op, other)

@pytest.mark.parametrize("box", [pd.Series, pd.DataFrame])
def test_direct_arith_with_ndframe_returns_not_implemented(self, data, box):
def test_direct_arith_with_ndframe_returns_not_implemented(
self, data, frame_or_series
Copy link
Member

Choose a reason for hiding this comment

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

Those base extension tests are used by downstream projects, so we should be a bit more careful with changing them (at least when for "clean up" purposes). Or use fixtures defined in tests/extension/conftest.py as you did in #44138.
I have changed back this specific line in #44332

):
# EAs should return NotImplemented for ops with Series/DataFrame
# Pandas takes care of unboxing the series and calling the EA's op.
other = pd.Series(data)
if box is pd.DataFrame:
if frame_or_series is pd.DataFrame:
other = other.to_frame()

if hasattr(data, "__eq__"):
Expand Down
Loading