Skip to content

Commit ad0d6e1

Browse files
fix warnings
1 parent f9ffff7 commit ad0d6e1

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

pandas/core/arrays/_arrow_string_mixins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ArrowStringArrayMixin:
3434
def __init__(self, *args, **kwargs) -> None:
3535
raise NotImplementedError
3636

37-
def _convert_bool_result(self, result, na=lib.no_default):
37+
def _convert_bool_result(self, result, na=lib.no_default, method_name=None):
3838
# Convert a bool-dtype result to the appropriate result type
3939
raise NotImplementedError
4040

@@ -130,7 +130,7 @@ def _str_startswith(
130130
and not isna(na)
131131
): # pyright: ignore [reportGeneralTypeIssues]
132132
result = result.fill_null(na)
133-
return self._convert_bool_result(result, na=na)
133+
return self._convert_bool_result(result, na=na, method_name="startswith")
134134

135135
def _str_endswith(
136136
self, pat: str | tuple[str, ...], na: Scalar | lib.NoDefault = lib.no_default
@@ -153,4 +153,4 @@ def _str_endswith(
153153
and not isna(na)
154154
): # pyright: ignore [reportGeneralTypeIssues]
155155
result = result.fill_null(na)
156-
return self._convert_bool_result(result, na=na)
156+
return self._convert_bool_result(result, na=na, method_name="endswith")

pandas/core/arrays/arrow/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,7 @@ def _apply_elementwise(self, func: Callable) -> list[list[Any]]:
23112311
for chunk in self._pa_array.iterchunks()
23122312
]
23132313

2314-
def _convert_bool_result(self, result, na=lib.no_default):
2314+
def _convert_bool_result(self, result, na=lib.no_default, method_name=None):
23152315
return type(self)(result)
23162316

23172317
def _convert_int_result(self, result):

pandas/core/arrays/string_arrow.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def insert(self, loc: int, item) -> ArrowStringArray:
223223
raise TypeError("Scalar must be NA or str")
224224
return super().insert(loc, item)
225225

226-
def _convert_bool_result(self, values, na=lib.no_default):
226+
def _convert_bool_result(self, values, na=lib.no_default, method_name=None):
227227
if self.dtype.na_value is np.nan:
228228
na_value: bool | lib.NoDefault
229229
if na is lib.no_default:
@@ -233,6 +233,14 @@ def _convert_bool_result(self, values, na=lib.no_default):
233233
values = values.fill_null(False)
234234
na_value = lib.no_default
235235
else:
236+
if not isinstance(na, bool):
237+
# GH#59561
238+
warnings.warn(
239+
f"Allowing a non-bool 'na' in obj.str.{method_name} is "
240+
"deprecated and will raise in a future version.",
241+
FutureWarning,
242+
stacklevel=find_stack_level(),
243+
)
236244
values = values.fill_null(bool(na))
237245
na_value = lib.no_default
238246
return ArrowExtensionArray(values).to_numpy(na_value=na_value)
@@ -310,7 +318,7 @@ def _str_contains(
310318
result = pc.match_substring_regex(self._pa_array, pat, ignore_case=not case)
311319
else:
312320
result = pc.match_substring(self._pa_array, pat, ignore_case=not case)
313-
result = self._convert_bool_result(result, na=na)
321+
result = self._convert_bool_result(result, na=na, method_name="contains")
314322
if (
315323
self.dtype.na_value is libmissing.NA
316324
and na is not lib.no_default

pandas/tests/strings/test_find_replace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ def test_startswith_endswith_validate_na(any_string_dtype):
299299

300300
dtype = ser.dtype
301301
if (
302-
isinstance(dtype, pd.StringDtype) and dtype.storage == "python"
302+
isinstance(dtype, pd.StringDtype)
303+
and (dtype.storage == "python" or dtype.na_value is np.nan)
303304
) or dtype == np.dtype("object"):
304305
msg = "Allowing a non-bool 'na' in obj.str.startswith is deprecated"
305306
with tm.assert_produces_warning(FutureWarning, match=msg):

0 commit comments

Comments
 (0)