Skip to content

Commit 11cc7e0

Browse files
authored
TST(string dtype): Resolve replace xfails (pandas-dev#60659)
* TST(string dtype): Resolve replace xfails * Add test * fixup
1 parent 8f9039d commit 11cc7e0

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

pandas/tests/frame/methods/test_replace.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ def test_regex_replace_str_to_numeric(self, mix_abc):
334334
return_value = res3.replace(regex=r"\s*\.\s*", value=0, inplace=True)
335335
assert return_value is None
336336
expec = DataFrame({"a": mix_abc["a"], "b": ["a", "b", 0, 0], "c": mix_abc["c"]})
337-
# TODO(infer_string)
338337
expec["c"] = expec["c"].astype(object)
339338
tm.assert_frame_equal(res, expec)
340339
tm.assert_frame_equal(res2, expec)
@@ -1476,21 +1475,24 @@ def test_regex_replace_scalar(
14761475
tm.assert_frame_equal(result, expected)
14771476

14781477
@pytest.mark.parametrize("regex", [False, True])
1479-
def test_replace_regex_dtype_frame(self, regex):
1478+
@pytest.mark.parametrize("value", [1, "1"])
1479+
def test_replace_regex_dtype_frame(self, regex, value):
14801480
# GH-48644
14811481
df1 = DataFrame({"A": ["0"], "B": ["0"]})
1482-
expected_df1 = DataFrame({"A": [1], "B": [1]}, dtype=object)
1483-
result_df1 = df1.replace(to_replace="0", value=1, regex=regex)
1482+
# When value is an integer, coerce result to object.
1483+
# When value is a string, infer the correct string dtype.
1484+
dtype = object if value == 1 else None
1485+
1486+
expected_df1 = DataFrame({"A": [value], "B": [value]}, dtype=dtype)
1487+
result_df1 = df1.replace(to_replace="0", value=value, regex=regex)
14841488
tm.assert_frame_equal(result_df1, expected_df1)
14851489

14861490
df2 = DataFrame({"A": ["0"], "B": ["1"]})
14871491
if regex:
1488-
# TODO(infer_string): both string columns get cast to object,
1489-
# while only needed for column A
1490-
expected_df2 = DataFrame({"A": [1], "B": ["1"]}, dtype=object)
1492+
expected_df2 = DataFrame({"A": [value], "B": ["1"]}, dtype=dtype)
14911493
else:
1492-
expected_df2 = DataFrame({"A": Series([1], dtype=object), "B": ["1"]})
1493-
result_df2 = df2.replace(to_replace="0", value=1, regex=regex)
1494+
expected_df2 = DataFrame({"A": Series([value], dtype=dtype), "B": ["1"]})
1495+
result_df2 = df2.replace(to_replace="0", value=value, regex=regex)
14941496
tm.assert_frame_equal(result_df2, expected_df2)
14951497

14961498
def test_replace_with_value_also_being_replaced(self):

0 commit comments

Comments
 (0)