Description
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:
- How to map True and False to 'Yes' and 'No' in a pandas data frame for columns of dtype bool only? (6k views, 6/8 upvotes) works around this problem by only using booleans in columns with
dtype == bool
- I wonder when replace 0 on a Series(or dataframe), will False also be replaced? demonstrates that replacing zeros will not touch booleans if the
dtype == bool
. - I verified that my problem exists with
dtype in {int, float}
(i.e. after removingTrue
from Series)
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