BUG: Unexpected behavior pandas.DataFrame.replace with "string" dtype · Issue #41333 · pandas-dev/pandas (original) (raw)


I'm trying to remove certain characters in a DataFrame, in this case, I am trying to remove the ñ.

Code Sample

df = pd.DataFrame([['añoos','asññ','ÑolÑÑss'],['añoos','asññ','ÑolÑÑss']], dtype='string') df.replace({'ñ':'n', 'Ñ':'N'}, regex = True, inplace = True)

The output is:

0 1 2
0 añoos asññ ÑolÑÑss
1 añoos asññ ÑolÑÑss

Problem description

If I do not specify the dtype to string, the output is the expected as follows:

Expected Output

df = pd.DataFrame([['añoos','asññ','ÑolÑÑss'],['añoos','asññ','ÑolÑÑss']]) df.replace({'ñ':'n', 'Ñ':'N'}, regex = True, inplace = True) print(df)

0 1 2
0 anoos asnn NolNNss
1 anoos asnn NolNNss

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : es_419.UTF-8
LOCALE : Spanish_Colombia.1252

pandas : 1.2.4
numpy : 1.19.4
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 54.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : None
pymysql : 0.9.3
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.6.0
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.5.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : 0.52.0