Repository: scikit-learn/scikit-learn
File: sklearn/preprocessing/tests/test_target_encoder.py (Python)

Code before:
    assert abs(coef[0]) < abs(coef[2])


def test_pandas_copy_on_write():
    """
    Test target-encoder cython code when y is read-only.

    The numpy array underlying df["y"] is read-only when copy-on-write is enabled.
    Non-regression test for gh-27879.
    """
    pd = pytest.importorskip("pandas", minversion="2.0")
    # Pandas currently warns that setting copy_on_write will be removed in pandas 4
    # (and copy-on-write will always be enabled).
    # see https://github.com/scikit-learn/scikit-learn/issues/32829
    # TODO: remove this workaround when pandas 4 is our minimum version
    if parse_version(pd.__version__) >= parse_version("4.0"):
        df = pd.DataFrame({"x": ["a", "b", "b"], "y": [4.0, 5.0, 6.0]})
        TargetEncoder(target_type="continuous").fit(df[["x"]], df["y"])
    else:
        with warnings.catch_warnings():
            warnings.filterwarnings(
                "ignore",
                message="Copy-on-Write can no longer be disabled, "
                "setting to False has no impact. This option will "
                "be removed in pandas 4.0.",
                category=pd.errors.Pandas4Warning,
            )
            with pd.option_context("mode.copy_on_write", True):
                df = pd.DataFrame({"x": ["a", "b", "b"], "y": [4.0, 5.0, 6.0]})
                TargetEncoder(target_type="continuous").fit(df[["x"]], df["y"])


Review comment (suggestion):
I took a look at the class hierarchy of the warning in the nightly build of pandas, and it is a subclass of the usual DeprecationWarning:

```python
>>> pd.errors.Pandas4Warning.__mro__ 
(pandas.errors.Pandas4Warning,
 pandas.errors.PandasDeprecationWarning,
 pandas.errors.PandasChangeWarning,
 DeprecationWarning,
 Warning,
 Exception,
 BaseException,
 object)
```

So filtering those might work, irrespective of the Pandas version.

```suggestion
                category=DeprecationWarning,
```

Code after:
    assert abs(coef[0]) < abs(coef[2])


def test_pandas_copy_on_write():
    """
    Test target-encoder cython code when y is read-only.

    The numpy array underlying df["y"] is read-only when copy-on-write is enabled.
    Non-regression test for gh-27879.
    """
    pd = pytest.importorskip("pandas", minversion="2.0")
    # Pandas currently warns that setting copy_on_write will be removed in pandas 4
    # (and copy-on-write will always be enabled).
    # see https://github.com/scikit-learn/scikit-learn/issues/32829
    # TODO: remove this workaround when pandas 4 is our minimum version
    if parse_version(pd.__version__) >= parse_version("4.0"):
        df = pd.DataFrame({"x": ["a", "b", "b"], "y": [4.0, 5.0, 6.0]})
        TargetEncoder(target_type="continuous").fit(df[["x"]], df["y"])
    else:
        with warnings.catch_warnings():
            expected_message = (
                "Copy-on-Write can no longer be disabled, "
                "setting to False has no impact. This option will "
                "be removed in pandas 4.0."
            )
            warnings.filterwarnings(
                "ignore",
                message=re.escape(expected_message),
                category=DeprecationWarning,
            )
            with pd.option_context("mode.copy_on_write", True):
                df = pd.DataFrame({"x": ["a", "b", "b"], "y": [4.0, 5.0, 6.0]})
                TargetEncoder(target_type="continuous").fit(df[["x"]], df["y"])