Skip to content

DataFrame.replace with dict behaves inconsistently for integers and strings #20656

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
Coldsp33d opened this issue Apr 11, 2018 · 5 comments · Fixed by #21477
Closed

DataFrame.replace with dict behaves inconsistently for integers and strings #20656

Coldsp33d opened this issue Apr 11, 2018 · 5 comments · Fixed by #21477
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Strings String extension data type and string data
Milestone

Comments

@Coldsp33d
Copy link

Coldsp33d commented Apr 11, 2018

From SO

DataFrame.replace does not behave consistently when replacing integers vs replacing strings.

Reproducible example:

df = pd.DataFrame({'A': [0, 1]})
df.A.replace({0: 1, 1: 0})

This yields the result:

    df
    A
    1
    0

However, when using the same commands for string values

df = pd.DataFrame({'B': ['a', 'b']})
df.B.replace({'a': 'b', 'b': 'a'})

This gives

df
B
a
a

But it should give

B
b
a

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.22.0
pytest: None
pip: 9.0.3
setuptools: 38.5.1
Cython: 0.28.1
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: None
xlrd: 0.9.4
xlwt: None
xlsxwriter: None
lxml: 3.8.0
bs4: 4.6.0
html5lib: 1.0b8
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@Coldsp33d Coldsp33d changed the title DataFrame.replace behaves inconsistently for integers and strings DataFrame.replace with dict behaves inconsistently for integers and strings Apr 11, 2018
@gfyoung gfyoung added Testing pandas testing functions or related to the test suite Can't Repro Bug and removed Can't Repro Testing pandas testing functions or related to the test suite labels Apr 15, 2018
@gfyoung
Copy link
Member

gfyoung commented Apr 15, 2018

@Coldsp33d : That is weird indeed. PR to patch is welcome!

@Licht-T
Copy link
Contributor

Licht-T commented May 8, 2018

Same problem happens in regex mode, but we need much refactor to fix everything (Licht-T@a1d141b). @gfyoung, is it okay to patch only normal replace for now?

@gfyoung
Copy link
Member

gfyoung commented May 8, 2018

Yeah, that's fine.

@peterpanmj
Copy link
Contributor

@Licht-T Is there an issue for regex replace similar to this one ?

@gfyoung gfyoung added Strings String extension data type and string data Compat pandas objects compatability with Numpy or Python functions labels Jun 15, 2018
@peterpanmj
Copy link
Contributor

@Licht-T I am working on this issue and I can solve for replace with string. Still, Regex replace is not solved. Maybe you can open another issue for regex replace. I think it takes more efforts to solve that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Strings String extension data type and string data
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants