BUG: Different results from Series.replace with compiled regex

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import re
import pandas as pd

print(pd.__version__)

s = pd.Series(['a', 'aa', 'b'])
regex = {'^a$': 1, '^b$': 2, '^aa$': 3}
print(s.replace(regex, regex=True)) 
print(s.replace({re.compile(k): v for k,v in regex.items()}, regex=True))

Problem description

These two .replace calls are equivalent on pandas 1.0.5. They are different on 1.1.0 and master.

1.2.0.dev0+79.g3c87b019b
0    1
1    3
2    2
dtype: int64
0     a
1    aa
2     b
dtype: object


1.0.5
0    1
1    3
2    2
dtype: int64
0    1
1    3
2    2
dtype: int64

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.0.5
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.1
setuptools : 49.3.1.post20200810
Cython : 0.29.21
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
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 : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

1 possible answer(s) on “BUG: Different results from Series.replace with compiled regex