Repository: pandas-dev/pandas
File: pandas/tests/strings/test_find_replace.py (Python)

Code before:


# --------------------------------------------------------------------------------------
# str.startswith
# --------------------------------------------------------------------------------------


def test_startswith_endswith_validate_na(any_string_dtype):
    # GH#59615
    ser = Series(
        ["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"],
        dtype=any_string_dtype,
    )

    dtype = ser.dtype
    if (
        isinstance(dtype, pd.StringDtype) and dtype.storage == "python"
    ) or dtype == np.dtype("object"):
        msg = "Allowing a non-bool 'na' in obj.str.startswith is deprecated"
        with tm.assert_produces_warning(FutureWarning, match=msg):
            ser.str.startswith("kapow", na="baz")
        msg = "Allowing a non-bool 'na' in obj.str.endswith is deprecated"
        with tm.assert_produces_warning(FutureWarning, match=msg):
            ser.str.endswith("bar", na="baz")
    else:
        # TODO: don't surface pyarrow errors
        import pyarrow as pa

        msg = "Could not convert 'baz' with type str: tried to convert to boolean"
        with pytest.raises(pa.lib.ArrowInvalid, match=msg):
            ser.str.startswith("kapow", na="baz")
        with pytest.raises(pa.lib.ArrowInvalid, match=msg):
            ser.str.endswith("kapow", na="baz")


@pytest.mark.parametrize("pat", ["foo", ("foo", "baz")])
@pytest.mark.parametrize("dtype", ["object", "category"])
@pytest.mark.parametrize("null_value", [None, np.nan, pd.NA])
@pytest.mark.parametrize("na", [True, False])
def test_startswith(pat, dtype, null_value, na):
    # add category dtype parametrizations for GH-36241
    values = Series(
        ["om", null_value, "foo_nom", "nom", "bar_foo", null_value, "foo"],
        dtype=dtype,
    )

    result = values.str.startswith(pat)
    exp = Series([False, np.nan, True, False, False, np.nan, True])
    if dtype == "object" and null_value is pd.NA:
        # GH#18463
        exp = exp.fillna(null_value)

Review comment (suggestion):
```suggestion
        # TODO(infer_string): don't surface pyarrow errors
```

Code after:


# --------------------------------------------------------------------------------------
# str.startswith
# --------------------------------------------------------------------------------------


def test_startswith_endswith_validate_na(any_string_dtype):
    # GH#59615
    ser = Series(
        ["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"],
        dtype=any_string_dtype,
    )

    dtype = ser.dtype
    if (
        isinstance(dtype, pd.StringDtype) and dtype.storage == "python"
    ) or dtype == np.dtype("object"):
        msg = "Allowing a non-bool 'na' in obj.str.startswith is deprecated"
        with tm.assert_produces_warning(FutureWarning, match=msg):
            ser.str.startswith("kapow", na="baz")
        msg = "Allowing a non-bool 'na' in obj.str.endswith is deprecated"
        with tm.assert_produces_warning(FutureWarning, match=msg):
            ser.str.endswith("bar", na="baz")
    else:
        # TODO(infer_string): don't surface pyarrow errors
        import pyarrow as pa

        msg = "Could not convert 'baz' with type str: tried to convert to boolean"
        with pytest.raises(pa.lib.ArrowInvalid, match=msg):
            ser.str.startswith("kapow", na="baz")
        with pytest.raises(pa.lib.ArrowInvalid, match=msg):
            ser.str.endswith("kapow", na="baz")


@pytest.mark.parametrize("pat", ["foo", ("foo", "baz")])
@pytest.mark.parametrize("dtype", ["object", "category"])
@pytest.mark.parametrize("null_value", [None, np.nan, pd.NA])
@pytest.mark.parametrize("na", [True, False])
def test_startswith(pat, dtype, null_value, na):
    # add category dtype parametrizations for GH-36241
    values = Series(
        ["om", null_value, "foo_nom", "nom", "bar_foo", null_value, "foo"],
        dtype=dtype,
    )

    result = values.str.startswith(pat)
    exp = Series([False, np.nan, True, False, False, np.nan, True])
    if dtype == "object" and null_value is pd.NA:
        # GH#18463
        exp = exp.fillna(null_value)