Skip to content

Commit

Permalink
DEPR: na validation for startswith, endswith
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Aug 27, 2024
1 parent d796a8e commit c53f154
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pandas/core/strings/object_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,26 @@ def _str_contains(

def _str_startswith(self, pat, na=None):
f = lambda x: x.startswith(pat)
if not isna(na) and not isinstance(na, bool):
# GH#59561
warnings.warn(
"Allowing a non-bool 'na' in obj.str.startswith is deprecated "
"and will raise in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
return self._str_map(f, na_value=na, dtype=np.dtype(bool))

def _str_endswith(self, pat, na=None):
f = lambda x: x.endswith(pat)
if not isna(na) and not isinstance(na, bool):
# GH#59561
warnings.warn(
"Allowing a non-bool 'na' in obj.str.endswith is deprecated "
"and will raise in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
return self._str_map(f, na_value=na, dtype=np.dtype(bool))

def _str_replace(
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/strings/test_find_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,34 @@ def test_contains_nan(any_string_dtype):
# --------------------------------------------------------------------------------------


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])
Expand Down

0 comments on commit c53f154

Please sign in to comment.