@@ -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