diff --git a/pandas/_testing.py b/pandas/_testing.py index 469f5e1bed6ba..73b1dcf31979f 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -108,6 +108,8 @@ + BYTES_DTYPES ) +NULL_OBJECTS = [None, np.nan, pd.NaT, float("nan"), pd.NA] + # set testing_mode _testing_mode_warnings = (DeprecationWarning, ResourceWarning) diff --git a/pandas/conftest.py b/pandas/conftest.py index 2bac2ed198789..d84a72d4cc7a8 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -266,7 +266,7 @@ def nselect_method(request): # ---------------------------------------------------------------- # Missing values & co. # ---------------------------------------------------------------- -@pytest.fixture(params=[None, np.nan, pd.NaT, float("nan"), pd.NA], ids=str) +@pytest.fixture(params=tm.NULL_OBJECTS, ids=str) def nulls_fixture(request): """ Fixture for each null type in pandas. diff --git a/pandas/tests/extension/arrow/test_bool.py b/pandas/tests/extension/arrow/test_bool.py index 922b3b94c16c1..b731859a761a4 100644 --- a/pandas/tests/extension/arrow/test_bool.py +++ b/pandas/tests/extension/arrow/test_bool.py @@ -51,8 +51,8 @@ def test_view(self, data): data.view() @pytest.mark.xfail(raises=AssertionError, reason="Not implemented yet") - def test_contains(self, data, data_missing, nulls_fixture): - super().test_contains(data, data_missing, nulls_fixture) + def test_contains(self, data, data_missing): + super().test_contains(data, data_missing) class TestConstructors(BaseArrowTests, base.BaseConstructorsTests): diff --git a/pandas/tests/extension/base/interface.py b/pandas/tests/extension/base/interface.py index d7997310dde3d..6a4ff68b4580f 100644 --- a/pandas/tests/extension/base/interface.py +++ b/pandas/tests/extension/base/interface.py @@ -29,7 +29,7 @@ def test_can_hold_na_valid(self, data): # GH-20761 assert data._can_hold_na is True - def test_contains(self, data, data_missing, nulls_fixture): + def test_contains(self, data, data_missing): # GH-37867 # Tests for membership checks. Membership checks for nan-likes is tricky and # the settled on rule is: `nan_like in arr` is True if nan_like is @@ -47,10 +47,12 @@ def test_contains(self, data, data_missing, nulls_fixture): assert na_value in data_missing assert na_value not in data - if nulls_fixture is not na_value: - # the data can never contain other nan-likes than na_value - assert nulls_fixture not in data - assert nulls_fixture not in data_missing + # the data can never contain other nan-likes than na_value + for na_value_obj in tm.NULL_OBJECTS: + if na_value_obj is na_value: + continue + assert na_value_obj not in data + assert na_value_obj not in data_missing def test_memory_usage(self, data): s = pd.Series(data) diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py index d03a9ab6b2588..4a0fb8f81ed56 100644 --- a/pandas/tests/extension/test_categorical.py +++ b/pandas/tests/extension/test_categorical.py @@ -87,7 +87,7 @@ def test_memory_usage(self, data): # Is this deliberate? super().test_memory_usage(data) - def test_contains(self, data, data_missing, nulls_fixture): + def test_contains(self, data, data_missing): # GH-37867 # na value handling in Categorical.__contains__ is deprecated. # See base.BaseInterFaceTests.test_contains for more details. @@ -105,9 +105,11 @@ def test_contains(self, data, data_missing, nulls_fixture): assert na_value not in data # Categoricals can contain other nan-likes than na_value - if nulls_fixture is not na_value: - assert nulls_fixture not in data - assert nulls_fixture in data_missing # this line differs from super method + for na_value_obj in tm.NULL_OBJECTS: + if na_value_obj is na_value: + continue + assert na_value_obj not in data + assert na_value_obj in data_missing # this line differs from super method class TestConstructors(base.BaseConstructorsTests):