Skip to content

Commit 4e139f6

Browse files
BUG: using .replace() on a Series containing np.nan raising ValueError when using regex=True. (#44653)
1 parent ef3237f commit 4e139f6

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ Other
824824
- Bug in :meth:`FloatingArray.equals` failing to consider two arrays equal if they contain ``np.nan`` values (:issue:`44382`)
825825
- Bug in :meth:`DataFrame.shift` with ``axis=1`` and ``ExtensionDtype`` columns incorrectly raising when an incompatible ``fill_value`` is passed (:issue:`44564`)
826826
- Bug in :meth:`DataFrame.diff` when passing a NumPy integer object instead of an ``int`` object (:issue:`44572`)
827-
-
827+
- Bug in :meth:`Series.replace` raising ``ValueError`` when using ``regex=True`` with a :class:`Series` containing ``np.nan`` values (:issue:`43344`)
828828

829829
.. ***DO NOT USE THIS SECTION***
830830

pandas/core/array_algos/replace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _check_comparison_types(
108108
# The shape of the mask can differ to that of the result
109109
# since we may compare only a subset of a's or b's elements
110110
tmp = np.zeros(mask.shape, dtype=np.bool_)
111-
tmp[mask] = result
111+
np.place(tmp, mask, result)
112112
result = tmp
113113

114114
_check_comparison_types(result, a, b)

pandas/tests/series/methods/test_replace.py

+13
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,16 @@ def test_replace_with_compiled_regex(self):
499499
result = s.replace({regex: "z"}, regex=True)
500500
expected = pd.Series(["z", "b", "c"])
501501
tm.assert_series_equal(result, expected)
502+
503+
def test_pandas_replace_na(self):
504+
# GH#43344
505+
ser = pd.Series(["AA", "BB", "CC", "DD", "EE", "", pd.NA], dtype="string")
506+
regex_mapping = {
507+
"AA": "CC",
508+
"BB": "CC",
509+
"EE": "CC",
510+
"CC": "CC-REPL",
511+
}
512+
result = ser.replace(regex_mapping, regex=True)
513+
exp = pd.Series(["CC", "CC", "CC-REPL", "DD", "CC", "", pd.NA], dtype="string")
514+
tm.assert_series_equal(result, exp)

0 commit comments

Comments
 (0)