Skip to content

BUG: Fix behavior of replace_list with mixed types. #40555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,7 @@ def can_hold_element(dtype: np.dtype, element: Any) -> bool:

if dtype.kind in ["i", "u"]:
if tipo is not None:
return tipo.kind in ["i", "u"] and dtype.itemsize >= tipo.itemsize
return tipo.kind in ["f", "i", "u"] and dtype.itemsize >= tipo.itemsize

# We have not inferred an integer from the dtype
# check if we have a builtin int or a float equal to an int
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,3 +1659,17 @@ def test_replace_bytes(self, frame_or_series):
expected = obj.copy()
obj = obj.replace({None: np.nan})
tm.assert_equal(obj, expected)

@pytest.mark.parametrize(
"s, to_replace, value, expected",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give this a multi-letter name instead of 's'

[
(DataFrame([1]), np.array([1.0]), [0], DataFrame([0])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to test this for different container types for to_replace rather than only np.array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added lists too. Should I add more types?

(DataFrame([1]), np.array([1]), [0], DataFrame([0])),
(DataFrame([1.0]), np.array([1.0]), [0], DataFrame([0.0])),
(DataFrame([1.0]), np.array([1]), [0], DataFrame([0.0])),
],
)
def test_replace_list_with_mixed_type(self, s, to_replace, value, expected):
# GH#40371
result = s.replace(to_replace, value)
tm.assert_frame_equal(result, expected)
14 changes: 14 additions & 0 deletions pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,17 @@ def test_str_replace_regex_default_raises_warning(self, pattern):
with tm.assert_produces_warning(FutureWarning) as w:
s.str.replace(pattern, "")
assert re.match(msg, str(w[0].message))

@pytest.mark.parametrize(
"s, to_replace, value, expected",
[
(pd.Series([1]), np.array([1.0]), [0], pd.Series([0])),
(pd.Series([1]), np.array([1]), [0], pd.Series([0])),
(pd.Series([1.0]), np.array([1.0]), [0], pd.Series([0.0])),
(pd.Series([1.0]), np.array([1]), [0], pd.Series([0.0])),
],
)
def test_replace_list_with_mixed_type(self, s, to_replace, value, expected):
# GH#40371
result = s.replace(to_replace, value)
tm.assert_series_equal(result, expected)