Skip to content

series.replace bug in .24 - regex not replacing patterns in middle of string (only beginning) #25530

Closed
@SigReaver

Description

@SigReaver

Code Sample, a copy-pastable example if possible

###sample code:
import pandas as pd
import re

s = pd.Series(['hello there', 'well hello then'])
print(s)

'''OUTPUT
0 hello there
1 well hello then
dtype: object
'''

s.replace({'hello' : 'goodbye'}, regex=True, inplace=True)

print(s)

'''OUTPUT
0 goodbye there
1 well hello then #<-not replaced
dtype: object
'''

###expected output using re.sub as per series.replace documentation
x = re.sub('hello','goodbye', 'hello there')
print(x)
#out: goodbye there

x = re.sub('hello','goodbye', 'well hello then')
print(x)

Problem description

.24 bug where series.replace is no longer working the same as in .23 pandas. In past versions, pandas would replace substrings that appeared in the middle/end of strings, but now it only seems to work if the pattern is at the very beginning.

I saw the docs mention that passing a dict and using regex follows re.sub, but when I tried that, I got the expected result (not the one .24 pandas yielded).

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

see above - should replace sub-strings in middle (not just beginning)

Output of pd.show_versions()

[paste the output of ``pd.show_versions()`` here below this line] INSTALLED VERSIONS ------------------ commit: None python: 3.7.0.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.24.1
pytest: 3.8.0
pip: 10.0.1
setuptools: 40.2.0
Cython: 0.28.5
numpy: 1.16.1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: 1.7.9
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: 2.5.6
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.1.0
lxml.etree: None
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
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