Skip to content

Commit 2fdf9a8

Browse files
BUG: using .replace() on a Series containing np.nan raising ValueError when using regex=True.
1 parent 8374c65 commit 2fdf9a8

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ Other
745745
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` with ``value=None`` and ExtensionDtypes (:issue:`44270`)
746746
- Bug in :meth:`FloatingArray.equals` failing to consider two arrays equal if they contain ``np.nan`` values (:issue:`44382`)
747747
- Bug in :meth:`DataFrame.diff` when passing a NumPy integer object instead of an ``int`` object (:issue:`44572`)
748-
-
748+
- Bug in :meth:`Series.replace` raising ``ValueError`` when using ``regex=True`` with a :class:`Series` containing ``np.nan`` values (:issue:`43344`)
749749

750750
.. ***DO NOT USE THIS SECTION***
751751

pandas/core/array_algos/replace.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 0 deletions
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)