Skip to content

replace() involving booleans affects integers and floats equal to 0 and 1 #31633

Open
@krassowski

Description

@krassowski

Code Sample

Series([0, 1, 2, True]).replace({0: 'a', True: 'x', False: 'y'}) 

Output:

0
0 y
1 x
2 2
3 x

Problem description

I expected zero to be replaced with 'a', and 1 to remain untouched.

  • documentation does not clarify whether replace() checks for equality or for identity.
  • both inexperienced and experienced users may fall in the trap of the code as shown above; if the current behaviour is desired, would it be reasonable to add a warning when the user's attempts can lead to certainly unintended results (as above)?

Expected Output

0
0 a
1 1
2 2
3 x

Due research

Stack Overflow:

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.0-26-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.0.0
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3.1
setuptools : 41.2.0
Cython : None
pytest : 5.2.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.8.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.2.1
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions