Skip to content

Commit 4eb3bed

Browse files
committed
DEPR: non-bool na for obj.str.contains
1 parent 360597c commit 4eb3bed

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

pandas/core/arrays/string_arrow.py

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
TYPE_CHECKING,
77
Union,
88
)
9+
import warnings
910

1011
import numpy as np
1112

@@ -19,6 +20,7 @@
1920
pa_version_under10p1,
2021
pa_version_under13p0,
2122
)
23+
from pandas.util._exceptions import find_stack_level
2224

2325
from pandas.core.dtypes.common import (
2426
is_scalar,
@@ -295,6 +297,14 @@ def _str_contains(
295297
result = pc.match_substring(self._pa_array, pat, ignore_case=not case)
296298
result = self._result_converter(result, na=na)
297299
if not isna(na):
300+
if not isinstance(na, bool):
301+
# GH#59561
302+
warnings.warn(
303+
"Allowing a non-bool 'na' in obj.str.contains is deprecated "
304+
"and will raise in a future version.",
305+
FutureWarning,
306+
stacklevel=find_stack_level(),
307+
)
298308
result[isna(result)] = bool(na)
299309
return result
300310

pandas/core/strings/object_array.py

+10
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
cast,
1010
)
1111
import unicodedata
12+
import warnings
1213

1314
import numpy as np
1415

1516
from pandas._libs import lib
1617
import pandas._libs.missing as libmissing
1718
import pandas._libs.ops as libops
19+
from pandas.util._exceptions import find_stack_level
1820

1921
from pandas.core.dtypes.missing import isna
2022

@@ -142,6 +144,14 @@ def _str_contains(
142144
else:
143145
upper_pat = pat.upper()
144146
f = lambda x: upper_pat in x.upper()
147+
if not isna(na) and not isinstance(na, bool):
148+
# GH#59561
149+
warnings.warn(
150+
"Allowing a non-bool 'na' in obj.str.contains is deprecated "
151+
"and will raise in a future version.",
152+
FutureWarning,
153+
stacklevel=find_stack_level(),
154+
)
145155
return self._str_map(f, na, dtype=np.dtype("bool"))
146156

147157
def _str_startswith(self, pat, na=None):

pandas/tests/strings/test_find_replace.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,16 @@ def test_contains_na_kwarg_for_nullable_string_dtype(
166166
# https://github.com/pandas-dev/pandas/pull/41025#issuecomment-824062416
167167

168168
values = Series(["a", "b", "c", "a", np.nan], dtype=nullable_string_dtype)
169-
result = values.str.contains("a", na=na, regex=regex)
169+
170+
msg = (
171+
"Allowing a non-bool 'na' in obj.str.contains is deprecated and "
172+
"will raise in a future version"
173+
)
174+
warn = None
175+
if not pd.isna(na) and not isinstance(na, bool):
176+
warn = FutureWarning
177+
with tm.assert_produces_warning(warn, match=msg):
178+
result = values.str.contains("a", na=na, regex=regex)
170179
expected = Series([True, False, False, True, expected], dtype="boolean")
171180
tm.assert_series_equal(result, expected)
172181

@@ -232,7 +241,12 @@ def test_contains_nan(any_string_dtype):
232241
expected = Series([True, True, True], dtype=expected_dtype)
233242
tm.assert_series_equal(result, expected)
234243

235-
result = s.str.contains("foo", na="foo")
244+
msg = (
245+
"Allowing a non-bool 'na' in obj.str.contains is deprecated and "
246+
"will raise in a future version"
247+
)
248+
with tm.assert_produces_warning(FutureWarning, match=msg):
249+
result = s.str.contains("foo", na="foo")
236250
if any_string_dtype == "object":
237251
expected = Series(["foo", "foo", "foo"], dtype=np.object_)
238252
elif any_string_dtype.na_value is np.nan:

0 commit comments

Comments
 (0)