Skip to content

Commit b29fc23

Browse files
committed
Only allow certain NA values
1 parent 17c4112 commit b29fc23

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

pandas/tests/strings/test_find_replace.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_contains_na_kwarg_for_nullable_string_dtype(
176176
values = Series(["a", "b", "c", "a", np.nan], dtype=nullable_string_dtype)
177177

178178
if na in [0, 3] and na is not False:
179-
msg = f"na must be an NA value, True, or False; got {na}"
179+
msg = f"na must be None, pd.NA, np.nan, True, or False; got {na}"
180180
with pytest.raises(ValueError, match=msg):
181181
values.str.contains("a", na=na, regex=regex)
182182
else:
@@ -252,7 +252,7 @@ def test_contains_nan(any_string_dtype):
252252
expected = Series([True, True, True], dtype=expected_dtype)
253253
tm.assert_series_equal(result, expected)
254254

255-
msg = "na must be an NA value, True, or False; got foo"
255+
msg = "na must be None, pd.NA, np.nan, True, or False; got foo"
256256
with pytest.raises(ValueError, match=msg):
257257
s.str.contains("foo", na="foo")
258258

@@ -339,7 +339,7 @@ def test_startswith_endswith_validate_na(any_string_dtype):
339339
["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"],
340340
dtype=any_string_dtype,
341341
)
342-
msg = "na must be an NA value, True, or False; got baz"
342+
msg = "na must be None, pd.NA, np.nan, True, or False; got baz"
343343
with pytest.raises(ValueError, match=msg):
344344
ser.str.startswith("kapow", na="baz")
345345
with pytest.raises(ValueError, match=msg):

pandas/util/_validators.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import numpy as np
1818

1919
from pandas._libs import lib
20+
from pandas._libs.missing import NA
2021

2122
from pandas.core.dtypes.common import (
2223
is_bool,
@@ -286,9 +287,15 @@ def validate_na_arg(value, name: str):
286287
ValueError
287288
When ``value`` is determined to be invalid.
288289
"""
289-
if value is lib.no_default or isinstance(value, bool) or isna(value):
290+
if (
291+
value is lib.no_default
292+
or isinstance(value, bool)
293+
or value is None
294+
or value is NA
295+
or (isna(value) and np.isnan(value))
296+
):
290297
return
291-
raise ValueError(f"{name} must be an NA value, True, or False; got {value}")
298+
raise ValueError(f"{name} must be None, pd.NA, np.nan, True, or False; got {value}")
292299

293300

294301
def validate_fillna_kwargs(value, method, validate_scalar_dict_value: bool = True):

0 commit comments

Comments
 (0)