Skip to content

Commit c53f154

Browse files
committed
DEPR: na validation for startswith, endswith
1 parent d796a8e commit c53f154

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

pandas/core/strings/object_array.py

+16
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,26 @@ def _str_contains(
156156

157157
def _str_startswith(self, pat, na=None):
158158
f = lambda x: x.startswith(pat)
159+
if not isna(na) and not isinstance(na, bool):
160+
# GH#59561
161+
warnings.warn(
162+
"Allowing a non-bool 'na' in obj.str.startswith is deprecated "
163+
"and will raise in a future version.",
164+
FutureWarning,
165+
stacklevel=find_stack_level(),
166+
)
159167
return self._str_map(f, na_value=na, dtype=np.dtype(bool))
160168

161169
def _str_endswith(self, pat, na=None):
162170
f = lambda x: x.endswith(pat)
171+
if not isna(na) and not isinstance(na, bool):
172+
# GH#59561
173+
warnings.warn(
174+
"Allowing a non-bool 'na' in obj.str.endswith is deprecated "
175+
"and will raise in a future version.",
176+
FutureWarning,
177+
stacklevel=find_stack_level(),
178+
)
163179
return self._str_map(f, na_value=na, dtype=np.dtype(bool))
164180

165181
def _str_replace(

pandas/tests/strings/test_find_replace.py

+28
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,34 @@ def test_contains_nan(any_string_dtype):
268268
# --------------------------------------------------------------------------------------
269269

270270

271+
def test_startswith_endswith_validate_na(any_string_dtype):
272+
# GH#59615
273+
ser = Series(
274+
["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"],
275+
dtype=any_string_dtype,
276+
)
277+
278+
dtype = ser.dtype
279+
if (
280+
isinstance(dtype, pd.StringDtype) and dtype.storage == "python"
281+
) or dtype == np.dtype("object"):
282+
msg = "Allowing a non-bool 'na' in obj.str.startswith is deprecated"
283+
with tm.assert_produces_warning(FutureWarning, match=msg):
284+
ser.str.startswith("kapow", na="baz")
285+
msg = "Allowing a non-bool 'na' in obj.str.endswith is deprecated"
286+
with tm.assert_produces_warning(FutureWarning, match=msg):
287+
ser.str.endswith("bar", na="baz")
288+
else:
289+
# TODO: don't surface pyarrow errors
290+
import pyarrow as pa
291+
292+
msg = "Could not convert 'baz' with type str: tried to convert to boolean"
293+
with pytest.raises(pa.lib.ArrowInvalid, match=msg):
294+
ser.str.startswith("kapow", na="baz")
295+
with pytest.raises(pa.lib.ArrowInvalid, match=msg):
296+
ser.str.endswith("kapow", na="baz")
297+
298+
271299
@pytest.mark.parametrize("pat", ["foo", ("foo", "baz")])
272300
@pytest.mark.parametrize("dtype", ["object", "category"])
273301
@pytest.mark.parametrize("null_value", [None, np.nan, pd.NA])

0 commit comments

Comments
 (0)