Skip to content

Commit

Permalink
BUG: DataFrame/Series regex replace fix for all NA values
Browse files Browse the repository at this point in the history
  • Loading branch information
snitish committed Jan 10, 2025
1 parent 0110487 commit 075fe67
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pandas/core/array_algos/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def _check_comparison_types(
op = np.vectorize(
lambda x: bool(re.search(b, x))
if isinstance(x, str) and isinstance(b, (str, Pattern))
else False
else False,
otypes=[bool],
)

# GH#32621 use mask to avoid comparing to NAs
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,13 @@ def test_replace_with_None_keeps_categorical(self):
)
tm.assert_frame_equal(result, expected)

def test_replace_all_NA(self):
# GH#60688
df = DataFrame({"ticker": ["#1234#"], "name": [None]})
result = df.replace({col: {r"^#": "$"} for col in df.columns}, regex=True)
expected = DataFrame({"ticker": ["$1234#"], "name": [None]})
tm.assert_frame_equal(result, expected)

def test_replace_value_is_none(self, datetime_frame):
orig_value = datetime_frame.iloc[0, 0]
orig2 = datetime_frame.iloc[1, 0]
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,10 @@ def test_replace_ea_float_with_bool(self):
expected = ser.copy()
result = ser.replace(0.0, True)
tm.assert_series_equal(result, expected)

def test_replace_all_NA(self):
# GH#60688
df = pd.Series([pd.NA, pd.NA])
result = df.replace({r"^#": "$"}, regex=True)
expected = pd.Series([pd.NA, pd.NA])
tm.assert_series_equal(result, expected)

0 comments on commit 075fe67

Please sign in to comment.