Skip to content
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

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

Closed
Coldsp33d opened this issue Apr 11, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@Coldsp33d
Copy link

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

This comment has been minimized.

Copy link
Member

commented Apr 15, 2018

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

@Licht-T

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented May 8, 2018

Yeah, that's fine.

@peterpanmj

This comment has been minimized.

Copy link
Contributor

commented Jun 8, 2018

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

@peterpanmj

This comment has been minimized.

Copy link
Contributor

commented Jun 15, 2018

@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
You can’t perform that action at this time.