Closed
Description
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
(optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
import pandas as pd
df = pd.DataFrame({"Y0": [1, 2], "Y1": [3, 4]})
df = df.replace({"replace_string": "test"})
Problem description
This raises a TypeError with the following Traceback:
Traceback (most recent call last):
File "/home/developer/.config/JetBrains/PyCharmCE2020.1/scratches/scratch_1.py", line 7, in <module>
df = df.replace({"replace_string": "test"})
File "/home/developer/PycharmProjects/pandas/pandas/core/frame.py", line 4277, in replace
return super().replace(
File "/home/developer/PycharmProjects/pandas/pandas/core/generic.py", line 6598, in replace
return self.replace(
File "/home/developer/PycharmProjects/pandas/pandas/core/frame.py", line 4277, in replace
return super().replace(
File "/home/developer/PycharmProjects/pandas/pandas/core/generic.py", line 6641, in replace
new_data = self._mgr.replace_list(
File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 616, in replace_list
masks = [comp(s, regex) for s in src_list]
File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 616, in <listcomp>
masks = [comp(s, regex) for s in src_list]
File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 614, in comp
return _compare_or_regex_search(values, s, regex)
File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 1946, in _compare_or_regex_search
_check_comparison_types(False, a, b)
File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 1925, in _check_comparison_types
raise TypeError(
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
Process finished with exit code 1
Expected Output
The original DataFrame.
If at least one column is from dtype object, the replace works, for example the following works:
import pandas as pd
df = pd.DataFrame({"Y0": [1, "2"], "Y1": [3, 4]})
df = df.replace({"replace_string": "test"})
Unexpectedly the following Code works:
import pandas as pd
df = pd.DataFrame({"Y0": [1, 2], "Y1": [3, 4]})
df = df.replace(to_replace="replace_string", value="test")
This is probably related to #16784
Tested on master.