Skip to content

BUG: Replace raises TypeError if to_replace is Dict with numeric DataFrame and key of Dict is String #34789

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

Closed
3 tasks done
phofl opened this issue Jun 15, 2020 · 0 comments · Fixed by #36093
Closed
3 tasks done
Labels
Bug replace replace method
Milestone

Comments

@phofl
Copy link
Member

phofl commented Jun 15, 2020

  • 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.

@phofl phofl added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 15, 2020
@TomAugspurger TomAugspurger added replace replace method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2020
@jreback jreback added this to the 1.2 milestone Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants